forked from jstejada/react-typist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
108 lines (95 loc) · 2.37 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React from 'react';
export function gaussianRnd(mean, std) {
const times = 12;
let sum = 0;
for (let idx = 0; idx < times; idx++) {
sum += Math.random();
}
sum -= (times / 2);
return Math.round((sum) * std) + mean;
}
export function asyncEach(arr, callback, onDone = ()=> {}) {
let count = 0;
const adv = ()=> {
if (count === arr.length) {
return onDone();
}
const idx = count;
count++;
callback(arr[idx], adv, idx);
};
adv();
}
export function eachRndTimeout(arr, callback, onDone, rndFn) {
asyncEach(arr, (el, adv, idx)=> {
callback(el, ()=> {
setTimeout(adv, rndFn(el, idx));
});
}, onDone);
}
export function exclude(obj, keys) {
const res = {};
for (const key in obj) {
if (keys.indexOf(key) === -1) {
res[key] = obj[key];
}
}
return res;
}
export function extractText(toType) {
const st = toType ? [toType] : [];
const lines = [];
while (st.length > 0) {
const cur = st.pop();
if (React.isValidElement(cur)) {
React.Children.forEach(cur.props.children, (child)=> {
st.push(child);
});
} else {
if (Array.isArray(cur)) {
for (const el of cur) {
st.push(el);
}
} else {
lines.unshift(cur);
}
}
}
return lines;
}
export function elementFactoryMaker() {
let key = 0;
return (el)=> {
const tag = el.type;
const props = exclude(el.props, ['children']);
props.key = `Typist-el-${key++}`;
return React.createElement.bind(null, tag, props);
};
}
export function extractTreeWithText(...args) {
if (!args[0]) return void(0);
const factMaker = elementFactoryMaker();
const inner = (tree, text, textIdx)=> {
if (textIdx >= text.length) return [null, textIdx];
let idx = textIdx;
const recurse = (ch)=> {
const [child, advIdx] = inner(ch, text, idx);
idx = advIdx;
return child;
};
// Recursively call on children of React Element
if (React.isValidElement(tree)) {
const fact = factMaker(tree);
const children = React.Children.map(tree.props.children, recurse) || [];
return [fact(...children), idx];
}
// Recursively call on array
if (Array.isArray(tree)) {
const children = tree.map(recurse);
return [children, idx];
}
// Return text
return [text[idx], idx + 1];
};
return inner(...args, 0)[0];
}