UNPKG

1.46 kBJavaScriptView Raw
1/** Copy own-properties from `props` onto `obj`.
2 * @returns obj
3 * @private
4 */
5export function extend(obj, props) {
6 if (props) {
7 for (let i in props) obj[i] = props[i];
8 }
9 return obj;
10}
11
12
13/** Fast clone. Note: does not filter out non-own properties.
14 * @see https://esbench.com/bench/56baa34f45df6895002e03b6
15 */
16export function clone(obj) {
17 return extend({}, obj);
18}
19
20
21/** Get a deep property value from the given object, expressed in dot-notation.
22 * @private
23 */
24export function delve(obj, key) {
25 for (let p=key.split('.'), i=0; i<p.length && obj; i++) {
26 obj = obj[p[i]];
27 }
28 return obj;
29}
30
31
32/** @private is the given object a Function? */
33export function isFunction(obj) {
34 return 'function'===typeof obj;
35}
36
37
38/** @private is the given object a String? */
39export function isString(obj) {
40 return 'string'===typeof obj;
41}
42
43
44/** Convert a hashmap of CSS classes to a space-delimited className string
45 * @private
46 */
47export function hashToClassName(c) {
48 let str = '';
49 for (let prop in c) {
50 if (c[prop]) {
51 if (str) str += ' ';
52 str += prop;
53 }
54 }
55 return str;
56}
57
58
59/** Just a memoized String#toLowerCase */
60let lcCache = {};
61export const toLowerCase = s => lcCache[s] || (lcCache[s] = s.toLowerCase());
62
63
64/** Call a function asynchronously, as soon as possible.
65 * @param {Function} callback
66 */
67let resolved = typeof Promise!=='undefined' && Promise.resolve();
68export const defer = resolved ? (f => { resolved.then(f); }) : setTimeout;