| 1 |
/** Checks if value is string */ |
| 2 |
function isString(str) { |
| 3 |
return typeof str === 'string' || str instanceof String; |
| 4 |
} |
| 5 |
|
| 6 |
/** Checks if value is object */ |
| 7 |
function isObject(obj) { |
| 8 |
var _obj$constructor; |
| 9 |
return typeof obj === 'object' && obj != null && (obj == null || (_obj$constructor = obj.constructor) == null ? void 0 : _obj$constructor.name) === 'Object'; |
| 10 |
} |
| 11 |
function pick(obj, keys) { |
| 12 |
if (Array.isArray(keys)) return pick(obj, (_, k) => keys.includes(k)); |
| 13 |
return Object.entries(obj).reduce((acc, _ref) => { |
| 14 |
let [k, v] = _ref; |
| 15 |
if (keys(v, k)) acc[k] = v; |
| 16 |
return acc; |
| 17 |
}, {}); |
| 18 |
} |
| 19 |
|
| 20 |
/** Direction */ |
| 21 |
const DIRECTION = { |
| 22 |
NONE: 'NONE', |
| 23 |
LEFT: 'LEFT', |
| 24 |
FORCE_LEFT: 'FORCE_LEFT', |
| 25 |
RIGHT: 'RIGHT', |
| 26 |
FORCE_RIGHT: 'FORCE_RIGHT' |
| 27 |
}; |
| 28 |
|
| 29 |
/** Direction */ |
| 30 |
|
| 31 |
function forceDirection(direction) { |
| 32 |
switch (direction) { |
| 33 |
case DIRECTION.LEFT: |
| 34 |
return DIRECTION.FORCE_LEFT; |
| 35 |
case DIRECTION.RIGHT: |
| 36 |
return DIRECTION.FORCE_RIGHT; |
| 37 |
default: |
| 38 |
return direction; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** Escapes regular expression control chars */ |
| 43 |
function escapeRegExp(str) { |
| 44 |
return str.replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1'); |
| 45 |
} |
| 46 |
|
| 47 |
// cloned from https://github.com/epoberezkin/fast-deep-equal with small changes |
| 48 |
function objectIncludes(b, a) { |
| 49 |
if (a === b) return true; |
| 50 |
const arrA = Array.isArray(a), |
| 51 |
arrB = Array.isArray(b); |
| 52 |
let i; |
| 53 |
if (arrA && arrB) { |
| 54 |
if (a.length != b.length) return false; |
| 55 |
for (i = 0; i < a.length; i++) if (!objectIncludes(a[i], b[i])) return false; |
| 56 |
return true; |
| 57 |
} |
| 58 |
if (arrA != arrB) return false; |
| 59 |
if (a && b && typeof a === 'object' && typeof b === 'object') { |
| 60 |
const dateA = a instanceof Date, |
| 61 |
dateB = b instanceof Date; |
| 62 |
if (dateA && dateB) return a.getTime() == b.getTime(); |
| 63 |
if (dateA != dateB) return false; |
| 64 |
const regexpA = a instanceof RegExp, |
| 65 |
regexpB = b instanceof RegExp; |
| 66 |
if (regexpA && regexpB) return a.toString() == b.toString(); |
| 67 |
if (regexpA != regexpB) return false; |
| 68 |
const keys = Object.keys(a); |
| 69 |
// if (keys.length !== Object.keys(b).length) return false; |
| 70 |
|
| 71 |
for (i = 0; i < keys.length; i++) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; |
| 72 |
for (i = 0; i < keys.length; i++) if (!objectIncludes(b[keys[i]], a[keys[i]])) return false; |
| 73 |
return true; |
| 74 |
} else if (a && b && typeof a === 'function' && typeof b === 'function') { |
| 75 |
return a.toString() === b.toString(); |
| 76 |
} |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
/** Selection range */ |
| 81 |
|
| 82 |
export { DIRECTION, escapeRegExp, forceDirection, isObject, isString, objectIncludes, pick }; |
| 83 |
|