UNPKG

modifyjs

Version:

Modify your objects with a mongo syntax.

53 lines (45 loc) 1.69 kB
import { isArray as underscoreIsArray, each } from 'underscore'; import { _f } from './selector'; var _ = { isArray: underscoreIsArray, each: each }; // Like _.isArray, but doesn't regard polyfilled Uint8Arrays on old browsers as // arrays. // XXX maybe this should be EJSON.isArray isArray = function isArray(x) { return _.isArray(x) && !EJSON.isBinary(x); }; export { isArray }; // XXX maybe this should be EJSON.isObject, though EJSON doesn't know about // RegExp // XXX note that _type(undefined) === 3!!!! isPlainObject = function isPlainObject(x) { return x && _f._type(x) === 3; }; export { isPlainObject }; isIndexable = function isIndexable(x) { return isArray(x) || isPlainObject(x); }; export { isIndexable }; // Returns true if this is an object with at least one key and all keys begin // with $. Unless inconsistentOK is set, throws if some keys begin with $ and // others don't. isOperatorObject = function isOperatorObject(valueSelector, inconsistentOK) { if (!isPlainObject(valueSelector)) return false; var theseAreOperators = undefined; _.each(valueSelector, function (value, selKey) { var thisIsOperator = selKey.substr(0, 1) === '$'; if (theseAreOperators === undefined) { theseAreOperators = thisIsOperator; } else if (theseAreOperators !== thisIsOperator) { if (!inconsistentOK) throw new Error("Inconsistent operator: " + JSON.stringify(valueSelector)); theseAreOperators = false; } }); return !!theseAreOperators; // {} has no operators }; export { isOperatorObject }; // string can be converted to integer isNumericKey = function isNumericKey(s) { return (/^[0-9]+$/.test(s) ); }; export { isNumericKey };