Comparing version 0.2.0 to 0.3.0
@@ -0,5 +1,7 @@ | ||
process.env.BABEL_ENV = 'dev'; | ||
module.exports = function (wallaby) { | ||
return { | ||
files: ['src/*.js', '!src/*.test.js'], | ||
files: ['src/**/*.js', '!src/*.test.js'], | ||
@@ -10,3 +12,3 @@ tests: ['src/*.test.js'], | ||
type: 'node', | ||
runner: 'node' | ||
runner: 'node', | ||
}, | ||
@@ -13,0 +15,0 @@ compilers: { |
@@ -1,65 +0,6 @@ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.EJSON = undefined; | ||
import { has, isNaN, size, isEmpty, any, each, all, isArguments, isArray } from 'underscore'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
var _base = require('./base64'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @namespace | ||
* @summary Namespace for EJSON functions | ||
*/ | ||
exports.EJSON = EJSON = {}; | ||
EJSON = {}; | ||
EJSONTest = {}; | ||
// Custom type interface definition | ||
/** | ||
* @class CustomType | ||
* @instanceName customType | ||
* @memberOf EJSON | ||
* @summary The interface that a class must satisfy to be able to become an | ||
* EJSON custom type via EJSON.addType. | ||
*/ | ||
/** | ||
* @function typeName | ||
* @memberOf EJSON.CustomType | ||
* @summary Return the tag used to identify this type. This must match the tag used to register this type with [`EJSON.addType`](#ejson_add_type). | ||
* @locus Anywhere | ||
* @instance | ||
*/ | ||
/** | ||
* @function toJSONValue | ||
* @memberOf EJSON.CustomType | ||
* @summary Serialize this instance into a JSON-compatible value. | ||
* @locus Anywhere | ||
* @instance | ||
*/ | ||
/** | ||
* @function clone | ||
* @memberOf EJSON.CustomType | ||
* @summary Return a value `r` such that `this.equals(r)` is true, and modifications to `r` do not affect `this` and vice versa. | ||
* @locus Anywhere | ||
* @instance | ||
*/ | ||
/** | ||
* @function equals | ||
* @memberOf EJSON.CustomType | ||
* @summary Return `true` if `other` has a value equal to `this`; `false` otherwise. | ||
* @locus Anywhere | ||
* @param {Object} other Another object to compare this to. | ||
* @instance | ||
*/ | ||
var customTypes = {}; | ||
@@ -84,263 +25,5 @@ // Add a custom type, using a method of your choice to get to and | ||
*/ | ||
EJSON.addType = function (name, factory) { | ||
if (_underscore2.default.has(customTypes, name)) throw new Error("Type " + name + " already present"); | ||
customTypes[name] = factory; | ||
}; | ||
var isInfOrNan = function isInfOrNan(obj) { | ||
return _underscore2.default.isNaN(obj) || obj === Infinity || obj === -Infinity; | ||
}; | ||
var _ = { has: has, isNaN: isNaN, size: size, isEmpty: isEmpty, any: any, each: each, all: all, isArguments: isArguments, isArray: isArray }; | ||
var builtinConverters = [{ // Date | ||
matchJSONValue: function matchJSONValue(obj) { | ||
return _underscore2.default.has(obj, '$date') && _underscore2.default.size(obj) === 1; | ||
}, | ||
matchObject: function matchObject(obj) { | ||
return obj instanceof Date; | ||
}, | ||
toJSONValue: function toJSONValue(obj) { | ||
return { $date: obj.getTime() }; | ||
}, | ||
fromJSONValue: function fromJSONValue(obj) { | ||
return new Date(obj.$date); | ||
} | ||
}, { // NaN, Inf, -Inf. (These are the only objects with typeof !== 'object' | ||
// which we match.) | ||
matchJSONValue: function matchJSONValue(obj) { | ||
return _underscore2.default.has(obj, '$InfNaN') && _underscore2.default.size(obj) === 1; | ||
}, | ||
matchObject: isInfOrNan, | ||
toJSONValue: function toJSONValue(obj) { | ||
var sign; | ||
if (_underscore2.default.isNaN(obj)) sign = 0;else if (obj === Infinity) sign = 1;else sign = -1; | ||
return { $InfNaN: sign }; | ||
}, | ||
fromJSONValue: function fromJSONValue(obj) { | ||
return obj.$InfNaN / 0; | ||
} | ||
}, { // Binary | ||
matchJSONValue: function matchJSONValue(obj) { | ||
return _underscore2.default.has(obj, '$binary') && _underscore2.default.size(obj) === 1; | ||
}, | ||
matchObject: function matchObject(obj) { | ||
return typeof Uint8Array !== 'undefined' && obj instanceof Uint8Array || obj && _underscore2.default.has(obj, '$Uint8ArrayPolyfill'); | ||
}, | ||
toJSONValue: function toJSONValue(obj) { | ||
return { $binary: _base.Base64.encode(obj) }; | ||
}, | ||
fromJSONValue: function fromJSONValue(obj) { | ||
return _base.Base64.decode(obj.$binary); | ||
} | ||
}, { // Escaping one level | ||
matchJSONValue: function matchJSONValue(obj) { | ||
return _underscore2.default.has(obj, '$escape') && _underscore2.default.size(obj) === 1; | ||
}, | ||
matchObject: function matchObject(obj) { | ||
if (_underscore2.default.isEmpty(obj) || _underscore2.default.size(obj) > 2) { | ||
return false; | ||
} | ||
return _underscore2.default.any(builtinConverters, function (converter) { | ||
return converter.matchJSONValue(obj); | ||
}); | ||
}, | ||
toJSONValue: function toJSONValue(obj) { | ||
var newObj = {}; | ||
_underscore2.default.each(obj, function (value, key) { | ||
newObj[key] = EJSON.toJSONValue(value); | ||
}); | ||
return { $escape: newObj }; | ||
}, | ||
fromJSONValue: function fromJSONValue(obj) { | ||
var newObj = {}; | ||
_underscore2.default.each(obj.$escape, function (value, key) { | ||
newObj[key] = EJSON.fromJSONValue(value); | ||
}); | ||
return newObj; | ||
} | ||
}, { // Custom | ||
matchJSONValue: function matchJSONValue(obj) { | ||
return _underscore2.default.has(obj, '$type') && _underscore2.default.has(obj, '$value') && _underscore2.default.size(obj) === 2; | ||
}, | ||
matchObject: function matchObject(obj) { | ||
return EJSON._isCustomType(obj); | ||
}, | ||
toJSONValue: function toJSONValue(obj) { | ||
var jsonValue = Meteor._noYieldsAllowed(function () { | ||
return obj.toJSONValue(); | ||
}); | ||
return { $type: obj.typeName(), $value: jsonValue }; | ||
}, | ||
fromJSONValue: function fromJSONValue(obj) { | ||
var typeName = obj.$type; | ||
if (!_underscore2.default.has(customTypes, typeName)) throw new Error("Custom EJSON type " + typeName + " is not defined"); | ||
var converter = customTypes[typeName]; | ||
return Meteor._noYieldsAllowed(function () { | ||
return converter(obj.$value); | ||
}); | ||
} | ||
}]; | ||
EJSON._isCustomType = function (obj) { | ||
return obj && typeof obj.toJSONValue === 'function' && typeof obj.typeName === 'function' && _underscore2.default.has(customTypes, obj.typeName()); | ||
}; | ||
EJSON._getTypes = function () { | ||
return customTypes; | ||
}; | ||
EJSON._getConverters = function () { | ||
return builtinConverters; | ||
}; | ||
// for both arrays and objects, in-place modification. | ||
var adjustTypesToJSONValue = EJSON._adjustTypesToJSONValue = function (obj) { | ||
// Is it an atom that we need to adjust? | ||
if (obj === null) return null; | ||
var maybeChanged = toJSONValueHelper(obj); | ||
if (maybeChanged !== undefined) return maybeChanged; | ||
// Other atoms are unchanged. | ||
if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') return obj; | ||
// Iterate over array or object structure. | ||
_underscore2.default.each(obj, function (value, key) { | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object' && value !== undefined && !isInfOrNan(value)) return; // continue | ||
var changed = toJSONValueHelper(value); | ||
if (changed) { | ||
obj[key] = changed; | ||
return; // on to the next key | ||
} | ||
// if we get here, value is an object but not adjustable | ||
// at this level. recurse. | ||
adjustTypesToJSONValue(value); | ||
}); | ||
return obj; | ||
}; | ||
// Either return the JSON-compatible version of the argument, or undefined (if | ||
// the item isn't itself replaceable, but maybe some fields in it are) | ||
var toJSONValueHelper = function toJSONValueHelper(item) { | ||
for (var i = 0; i < builtinConverters.length; i++) { | ||
var converter = builtinConverters[i]; | ||
if (converter.matchObject(item)) { | ||
return converter.toJSONValue(item); | ||
} | ||
} | ||
return undefined; | ||
}; | ||
/** | ||
* @summary Serialize an EJSON-compatible value into its plain JSON representation. | ||
* @locus Anywhere | ||
* @param {EJSON} val A value to serialize to plain JSON. | ||
*/ | ||
EJSON.toJSONValue = function (item) { | ||
var changed = toJSONValueHelper(item); | ||
if (changed !== undefined) return changed; | ||
if ((typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object') { | ||
item = EJSON.clone(item); | ||
adjustTypesToJSONValue(item); | ||
} | ||
return item; | ||
}; | ||
// for both arrays and objects. Tries its best to just | ||
// use the object you hand it, but may return something | ||
// different if the object you hand it itself needs changing. | ||
// | ||
var adjustTypesFromJSONValue = EJSON._adjustTypesFromJSONValue = function (obj) { | ||
if (obj === null) return null; | ||
var maybeChanged = fromJSONValueHelper(obj); | ||
if (maybeChanged !== obj) return maybeChanged; | ||
// Other atoms are unchanged. | ||
if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') return obj; | ||
_underscore2.default.each(obj, function (value, key) { | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
var changed = fromJSONValueHelper(value); | ||
if (value !== changed) { | ||
obj[key] = changed; | ||
return; | ||
} | ||
// if we get here, value is an object but not adjustable | ||
// at this level. recurse. | ||
adjustTypesFromJSONValue(value); | ||
} | ||
}); | ||
return obj; | ||
}; | ||
// Either return the argument changed to have the non-json | ||
// rep of itself (the Object version) or the argument itself. | ||
// DOES NOT RECURSE. For actually getting the fully-changed value, use | ||
// EJSON.fromJSONValue | ||
var fromJSONValueHelper = function fromJSONValueHelper(value) { | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null) { | ||
if (_underscore2.default.size(value) <= 2 && _underscore2.default.all(value, function (v, k) { | ||
return typeof k === 'string' && k.substr(0, 1) === '$'; | ||
})) { | ||
for (var i = 0; i < builtinConverters.length; i++) { | ||
var converter = builtinConverters[i]; | ||
if (converter.matchJSONValue(value)) { | ||
return converter.fromJSONValue(value); | ||
} | ||
} | ||
} | ||
} | ||
return value; | ||
}; | ||
/** | ||
* @summary Deserialize an EJSON value from its plain JSON representation. | ||
* @locus Anywhere | ||
* @param {JSONCompatible} val A value to deserialize into EJSON. | ||
*/ | ||
EJSON.fromJSONValue = function (item) { | ||
var changed = fromJSONValueHelper(item); | ||
if (changed === item && (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object') { | ||
item = EJSON.clone(item); | ||
adjustTypesFromJSONValue(item); | ||
return item; | ||
} else { | ||
return changed; | ||
} | ||
}; | ||
/** | ||
* @summary Serialize a value to a string. | ||
For EJSON values, the serialization fully represents the value. For non-EJSON values, serializes the same way as `JSON.stringify`. | ||
* @locus Anywhere | ||
* @param {EJSON} val A value to stringify. | ||
* @param {Object} [options] | ||
* @param {Boolean | Integer | String} options.indent Indents objects and arrays for easy readability. When `true`, indents by 2 spaces; when an integer, indents by that number of spaces; and when a string, uses the string as the indentation pattern. | ||
* @param {Boolean} options.canonical When `true`, stringifies keys in an object in sorted order. | ||
*/ | ||
EJSON.stringify = function (item, options) { | ||
var json = EJSON.toJSONValue(item); | ||
if (options && (options.canonical || options.indent)) { | ||
return EJSON._canonicalStringify(json, options); | ||
} else { | ||
return JSON.stringify(json); | ||
} | ||
}; | ||
/** | ||
* @summary Parse a string into an EJSON value. Throws an error if the string is not valid EJSON. | ||
* @locus Anywhere | ||
* @param {String} str A string to parse into an EJSON value. | ||
*/ | ||
EJSON.parse = function (item) { | ||
if (typeof item !== 'string') throw new Error("EJSON.parse argument should be a string"); | ||
return EJSON.fromJSONValue(JSON.parse(item)); | ||
}; | ||
/** | ||
* @summary Returns true if `x` is a buffer of binary data, as returned from [`EJSON.newBinary`](#ejson_new_binary). | ||
* @param {Object} x The variable to check. | ||
* @locus Anywhere | ||
*/ | ||
EJSON.isBinary = function (obj) { | ||
@@ -362,7 +45,7 @@ return !!(typeof Uint8Array !== 'undefined' && obj instanceof Uint8Array || obj && obj.$Uint8ArrayPolyfill); | ||
if (a === b) return true; | ||
if (_underscore2.default.isNaN(a) && _underscore2.default.isNaN(b)) return true; // This differs from the IEEE spec for NaN equality, b/c we don't want | ||
if (_.isNaN(a) && _.isNaN(b)) return true; // This differs from the IEEE spec for NaN equality, b/c we don't want | ||
// anything ever with a NaN to be poisoned from becoming equal to anything. | ||
if (!a || !b) // if either one is falsy, they'd have to be === to be equal | ||
return false; | ||
if (!((typeof a === 'undefined' ? 'undefined' : _typeof(a)) === 'object' && (typeof b === 'undefined' ? 'undefined' : _typeof(b)) === 'object')) return false; | ||
if (!((typeof a === 'undefined' ? 'undefined' : babelHelpers.typeof(a)) === 'object' && (typeof b === 'undefined' ? 'undefined' : babelHelpers.typeof(b)) === 'object')) return false; | ||
if (a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf(); | ||
@@ -386,9 +69,3 @@ if (EJSON.isBinary(a) && EJSON.isBinary(b)) { | ||
} | ||
// fallback for custom types that don't implement their own equals | ||
switch (EJSON._isCustomType(a) + EJSON._isCustomType(b)) { | ||
case 1: | ||
return false; | ||
case 2: | ||
return EJSON.equals(EJSON.toJSONValue(a), EJSON.toJSONValue(b)); | ||
} | ||
// fall back to structural equality of objects | ||
@@ -398,7 +75,7 @@ var ret; | ||
var bKeys = []; | ||
_underscore2.default.each(b, function (val, x) { | ||
_.each(b, function (val, x) { | ||
bKeys.push(x); | ||
}); | ||
i = 0; | ||
ret = _underscore2.default.all(a, function (val, x) { | ||
ret = _.all(a, function (val, x) { | ||
if (i >= bKeys.length) { | ||
@@ -419,4 +96,4 @@ return false; | ||
i = 0; | ||
ret = _underscore2.default.all(a, function (val, key) { | ||
if (!_underscore2.default.has(b, key)) { | ||
ret = _.all(a, function (val, key) { | ||
if (!_.has(b, key)) { | ||
return false; | ||
@@ -430,3 +107,3 @@ } | ||
}); | ||
return ret && _underscore2.default.size(b) === i; | ||
return ret && _.size(b) === i; | ||
} | ||
@@ -442,3 +119,3 @@ }; | ||
var ret; | ||
if ((typeof v === 'undefined' ? 'undefined' : _typeof(v)) !== "object") return v; | ||
if ((typeof v === 'undefined' ? 'undefined' : babelHelpers.typeof(v)) !== "object") return v; | ||
if (v === null) return null; // null has typeof "object" | ||
@@ -457,3 +134,3 @@ if (v instanceof Date) return new Date(v.getTime()); | ||
// XXX: Use something better than underscore's isArray | ||
if (_underscore2.default.isArray(v) || _underscore2.default.isArguments(v)) { | ||
if (_.isArray(v) || _.isArguments(v)) { | ||
// For some reason, _.map doesn't work in this context on Opera (weird test | ||
@@ -470,9 +147,6 @@ // failures). | ||
} | ||
// handle other custom types | ||
if (EJSON._isCustomType(v)) { | ||
return EJSON.fromJSONValue(EJSON.clone(EJSON.toJSONValue(v)), true); | ||
} | ||
// handle other objects | ||
ret = {}; | ||
_underscore2.default.each(v, function (value, key) { | ||
_.each(v, function (value, key) { | ||
ret[key] = EJSON.clone(value); | ||
@@ -483,14 +157,2 @@ }); | ||
/** | ||
* @summary Allocate a new buffer of binary data that EJSON can serialize. | ||
* @locus Anywhere | ||
* @param {Number} size The number of bytes of binary data to allocate. | ||
*/ | ||
// EJSON.newBinary is the public documented API for this functionality, | ||
// but the implementation is in the 'base64' package to avoid | ||
// introducing a circular dependency. (If the implementation were here, | ||
// then 'base64' would have to use EJSON.newBinary, and 'ejson' would | ||
// also have to use 'base64'.) | ||
EJSON.newBinary = _base.Base64.newBinary; | ||
exports.EJSON = EJSON; | ||
export { EJSON }; |
@@ -1,21 +0,12 @@ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isNumericKey = exports.isOperatorObject = exports.isIndexable = exports.isPlainObject = exports.isArray = undefined; | ||
import { isArray as underscoreIsArray, each } from 'underscore'; | ||
import { _f } from './selector'; | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
var _selector = require('./selector'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
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 | ||
exports.isArray = isArray = function isArray(x) { | ||
return _underscore2.default.isArray(x) && !EJSON.isBinary(x); | ||
isArray = function isArray(x) { | ||
return _.isArray(x) && !EJSON.isBinary(x); | ||
}; | ||
exports.isArray = isArray; | ||
export { isArray }; | ||
@@ -25,13 +16,11 @@ // XXX maybe this should be EJSON.isObject, though EJSON doesn't know about | ||
// XXX note that _type(undefined) === 3!!!! | ||
exports.isPlainObject = isPlainObject = function isPlainObject(x) { | ||
return x && _selector._f._type(x) === 3; | ||
isPlainObject = function isPlainObject(x) { | ||
return x && _f._type(x) === 3; | ||
}; | ||
exports.isPlainObject = isPlainObject; | ||
export { isPlainObject }; | ||
exports.isIndexable = isIndexable = function isIndexable(x) { | ||
isIndexable = function isIndexable(x) { | ||
return isArray(x) || isPlainObject(x); | ||
}; | ||
exports.isIndexable = isIndexable; | ||
export { isIndexable }; | ||
@@ -41,8 +30,7 @@ // Returns true if this is an object with at least one key and all keys begin | ||
// others don't. | ||
exports.isOperatorObject = isOperatorObject = function isOperatorObject(valueSelector, inconsistentOK) { | ||
isOperatorObject = function isOperatorObject(valueSelector, inconsistentOK) { | ||
if (!isPlainObject(valueSelector)) return false; | ||
var theseAreOperators = undefined; | ||
_underscore2.default.each(valueSelector, function (value, selKey) { | ||
_.each(valueSelector, function (value, selKey) { | ||
var thisIsOperator = selKey.substr(0, 1) === '$'; | ||
@@ -59,7 +47,6 @@ if (theseAreOperators === undefined) { | ||
exports.isOperatorObject = isOperatorObject; | ||
export { isOperatorObject }; | ||
// string can be converted to integer | ||
exports.isNumericKey = isNumericKey = function isNumericKey(s) { | ||
isNumericKey = function isNumericKey(s) { | ||
return (/^[0-9]+$/.test(s) | ||
@@ -69,2 +56,2 @@ ); | ||
exports.isNumericKey = isNumericKey; | ||
export { isNumericKey }; |
@@ -1,4 +0,1 @@ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
MinimongoError = function MinimongoError(message) { | ||
@@ -16,2 +13,2 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
exports.default = MinimongoError; | ||
export default MinimongoError; |
@@ -1,9 +0,9 @@ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
import { all, each, keys, has } from 'underscore'; | ||
import isObject from 'lodash-es/isObject'; | ||
import { assertHasValidFieldNames, assertIsValidFieldName } from './validation.js'; | ||
import { isPlainObject, isOperatorObject, isIndexable, isNumericKey } from './helpers'; | ||
import { _f } from './selector'; | ||
import MinimongoError from './MinimongoError'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _ = { all: all, each: each, keys: keys, has: has, isObject: isObject }; | ||
// XXX need a strategy for passing the binding of $ into this | ||
@@ -21,29 +21,11 @@ // function, from the compiled selector | ||
// out when to set the fields in $setOnInsert, if present. | ||
export default function (doc, mod, options) { | ||
return LocalCollection._modify(doc, mod, babelHelpers.extends({}, options, { returnInsteadOfReplacing: true })); | ||
} | ||
LocalCollection = window && window.LocalCollection || global && global.LocalCollection || {}; | ||
exports.default = function (doc, mod, options) { | ||
return LocalCollection._modify(doc, mod, _extends({}, options, { returnInsteadOfReplacing: true })); | ||
}; | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
var _validation = require('./validation.js'); | ||
var _helpers = require('./helpers'); | ||
var _selector = require('./selector'); | ||
var _MinimongoError = require('./MinimongoError'); | ||
var _MinimongoError2 = _interopRequireDefault(_MinimongoError); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
LocalCollection = global.LocalCollection || {}; | ||
LocalCollection._modify = function (doc, mod, options) { | ||
options = options || {}; | ||
if (!(0, _helpers.isPlainObject)(mod)) throw (0, _MinimongoError2.default)("Modifier must be an object"); | ||
if (!isPlainObject(mod)) throw MinimongoError("Modifier must be an object"); | ||
@@ -53,3 +35,3 @@ // Make sure the caller can't mutate our data structures. | ||
var isModifier = (0, _helpers.isOperatorObject)(mod); | ||
var isModifier = isOperatorObject(mod); | ||
@@ -59,6 +41,4 @@ var newDoc; | ||
if (!isModifier) { | ||
if (mod._id && !EJSON.equals(doc._id, mod._id)) throw (0, _MinimongoError2.default)("Cannot change the _id of a document"); | ||
// replace the whole document | ||
(0, _validation.assertHasValidFieldNames)(mod); | ||
assertHasValidFieldNames(mod); | ||
newDoc = mod; | ||
@@ -68,23 +48,17 @@ } else { | ||
newDoc = EJSON.clone(doc); | ||
_underscore2.default.each(mod, function (operand, op) { | ||
_.each(mod, function (operand, op) { | ||
var modFunc = MODIFIERS[op]; | ||
// Treat $setOnInsert as $set if this is an insert. | ||
if (options.isInsert && op === '$setOnInsert') modFunc = MODIFIERS['$set']; | ||
if (!modFunc) throw (0, _MinimongoError2.default)("Invalid modifier specified " + op); | ||
_underscore2.default.each(operand, function (arg, keypath) { | ||
if (!modFunc) throw MinimongoError("Invalid modifier specified " + op); | ||
_.each(operand, function (arg, keypath) { | ||
if (keypath === '') { | ||
throw (0, _MinimongoError2.default)("An empty update path is not valid."); | ||
throw MinimongoError("An empty update path is not valid."); | ||
} | ||
if (keypath === '_id' && op !== '$setOnInsert') { | ||
throw (0, _MinimongoError2.default)("Mod on _id not allowed"); | ||
} | ||
var keyparts = keypath.split('.'); | ||
if (!_underscore2.default.all(keyparts, _underscore2.default.identity)) { | ||
throw (0, _MinimongoError2.default)("The update path '" + keypath + "' contains an empty field name, which is not allowed."); | ||
if (!all(keyparts)) { | ||
throw MinimongoError("The update path '" + keypath + "' contains an empty field name, which is not allowed."); | ||
} | ||
var noCreate = _underscore2.default.has(NO_CREATE_MODIFIERS, op); | ||
var noCreate = _.has(NO_CREATE_MODIFIERS, op); | ||
var forbidArray = op === "$rename"; | ||
@@ -106,3 +80,3 @@ var target = findModTarget(newDoc, keyparts, { | ||
// move new document into place. | ||
_underscore2.default.each(_underscore2.default.keys(doc), function (k) { | ||
_.each(_.keys(doc), function (k) { | ||
// Note: this used to be for (var k in doc) however, this does not | ||
@@ -113,3 +87,3 @@ // work right in Opera. Deleting from a doc while iterating over it | ||
}); | ||
_underscore2.default.each(newDoc, function (v, k) { | ||
_.each(newDoc, function (v, k) { | ||
doc[k] = v; | ||
@@ -143,6 +117,6 @@ }); | ||
var keypart = keyparts[i]; | ||
var indexable = (0, _helpers.isIndexable)(doc); | ||
var indexable = isIndexable(doc); | ||
if (!indexable) { | ||
if (options.noCreate) return undefined; | ||
var e = (0, _MinimongoError2.default)("cannot use the part '" + keypart + "' to traverse " + doc); | ||
var e = MinimongoError("cannot use the part '" + keypart + "' to traverse " + doc); | ||
e.setPropertyError = true; | ||
@@ -154,13 +128,13 @@ throw e; | ||
if (keypart === '$') { | ||
if (usedArrayIndex) throw (0, _MinimongoError2.default)("Too many positional (i.e. '$') elements"); | ||
if (usedArrayIndex) throw MinimongoError("Too many positional (i.e. '$') elements"); | ||
if (!options.arrayIndices || !options.arrayIndices.length) { | ||
throw (0, _MinimongoError2.default)("The positional operator did not find the " + "match needed from the query"); | ||
throw MinimongoError("The positional operator did not find the " + "match needed from the query"); | ||
} | ||
keypart = options.arrayIndices[0]; | ||
usedArrayIndex = true; | ||
} else if ((0, _helpers.isNumericKey)(keypart)) { | ||
} else if (isNumericKey(keypart)) { | ||
keypart = parseInt(keypart); | ||
} else { | ||
if (options.noCreate) return undefined; | ||
throw (0, _MinimongoError2.default)("can't append to array using string field name [" + keypart + "]"); | ||
throw MinimongoError("can't append to array using string field name [" + keypart + "]"); | ||
} | ||
@@ -174,6 +148,6 @@ if (last) | ||
}if (!last) { | ||
if (doc.length === keypart) doc.push({});else if (_typeof(doc[keypart]) !== "object") throw (0, _MinimongoError2.default)("can't modify field '" + keyparts[i + 1] + "' of list value " + JSON.stringify(doc[keypart])); | ||
if (doc.length === keypart) doc.push({});else if (babelHelpers.typeof(doc[keypart]) !== "object") throw MinimongoError("can't modify field '" + keyparts[i + 1] + "' of list value " + JSON.stringify(doc[keypart])); | ||
} | ||
} else { | ||
(0, _validation.assertIsValidFieldName)(keypart); | ||
assertIsValidFieldName(keypart); | ||
if (!(keypart in doc)) { | ||
@@ -202,8 +176,8 @@ if (options.noCreate) return undefined; | ||
$currentDate: function $currentDate(target, field, arg) { | ||
if ((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === "object" && arg.hasOwnProperty("$type")) { | ||
if ((typeof arg === 'undefined' ? 'undefined' : babelHelpers.typeof(arg)) === "object" && arg.hasOwnProperty("$type")) { | ||
if (arg.$type !== "date") { | ||
throw (0, _MinimongoError2.default)("Minimongo does currently only support the date type " + "in $currentDate modifiers", { field: field }); | ||
throw MinimongoError("Minimongo does currently only support the date type " + "in $currentDate modifiers", { field: field }); | ||
} | ||
} else if (arg !== true) { | ||
throw (0, _MinimongoError2.default)("Invalid $currentDate modifier", { field: field }); | ||
throw MinimongoError("Invalid $currentDate modifier", { field: field }); | ||
} | ||
@@ -214,7 +188,7 @@ target[field] = new Date(); | ||
if (typeof arg !== "number") { | ||
throw (0, _MinimongoError2.default)("Modifier $min allowed for numbers only", { field: field }); | ||
throw MinimongoError("Modifier $min allowed for numbers only", { field: field }); | ||
} | ||
if (field in target) { | ||
if (typeof target[field] !== "number") { | ||
throw (0, _MinimongoError2.default)("Cannot apply $min modifier to non-number", { field: field }); | ||
throw MinimongoError("Cannot apply $min modifier to non-number", { field: field }); | ||
} | ||
@@ -230,7 +204,7 @@ if (target[field] > arg) { | ||
if (typeof arg !== "number") { | ||
throw (0, _MinimongoError2.default)("Modifier $max allowed for numbers only", { field: field }); | ||
throw MinimongoError("Modifier $max allowed for numbers only", { field: field }); | ||
} | ||
if (field in target) { | ||
if (typeof target[field] !== "number") { | ||
throw (0, _MinimongoError2.default)("Cannot apply $max modifier to non-number", { field: field }); | ||
throw MinimongoError("Cannot apply $max modifier to non-number", { field: field }); | ||
} | ||
@@ -245,5 +219,5 @@ if (target[field] < arg) { | ||
$inc: function $inc(target, field, arg) { | ||
if (typeof arg !== "number") throw (0, _MinimongoError2.default)("Modifier $inc allowed for numbers only", { field: field }); | ||
if (typeof arg !== "number") throw MinimongoError("Modifier $inc allowed for numbers only", { field: field }); | ||
if (field in target) { | ||
if (typeof target[field] !== "number") throw (0, _MinimongoError2.default)("Cannot apply $inc modifier to non-number", { field: field }); | ||
if (typeof target[field] !== "number") throw MinimongoError("Cannot apply $inc modifier to non-number", { field: field }); | ||
target[field] += arg; | ||
@@ -255,5 +229,5 @@ } else { | ||
$set: function $set(target, field, arg) { | ||
if (!_underscore2.default.isObject(target)) { | ||
if (!_.isObject(target)) { | ||
// not an array or an object | ||
var e = (0, _MinimongoError2.default)("Cannot set property on non-object field", { field: field }); | ||
var e = MinimongoError("Cannot set property on non-object field", { field: field }); | ||
e.setPropertyError = true; | ||
@@ -263,7 +237,7 @@ throw e; | ||
if (target === null) { | ||
var e = (0, _MinimongoError2.default)("Cannot set property on null", { field: field }); | ||
var e = MinimongoError("Cannot set property on null", { field: field }); | ||
e.setPropertyError = true; | ||
throw e; | ||
} | ||
(0, _validation.assertHasValidFieldNames)(arg); | ||
assertHasValidFieldNames(arg); | ||
target[field] = arg; | ||
@@ -283,7 +257,7 @@ }, | ||
if (target[field] === undefined) target[field] = []; | ||
if (!(target[field] instanceof Array)) throw (0, _MinimongoError2.default)("Cannot apply $push modifier to non-array", { field: field }); | ||
if (!(target[field] instanceof Array)) throw MinimongoError("Cannot apply $push modifier to non-array", { field: field }); | ||
if (!(arg && arg.$each)) { | ||
// Simple mode: not $each | ||
(0, _validation.assertHasValidFieldNames)(arg); | ||
assertHasValidFieldNames(arg); | ||
target[field].push(arg); | ||
@@ -295,4 +269,4 @@ return; | ||
var toPush = arg.$each; | ||
if (!(toPush instanceof Array)) throw (0, _MinimongoError2.default)("$each must be an array", { field: field }); | ||
(0, _validation.assertHasValidFieldNames)(toPush); | ||
if (!(toPush instanceof Array)) throw MinimongoError("$each must be an array", { field: field }); | ||
assertHasValidFieldNames(toPush); | ||
@@ -302,5 +276,5 @@ // Parse $position | ||
if ('$position' in arg) { | ||
if (typeof arg.$position !== "number") throw (0, _MinimongoError2.default)("$position must be a numeric value", { field: field }); | ||
if (typeof arg.$position !== "number") throw MinimongoError("$position must be a numeric value", { field: field }); | ||
// XXX should check to make sure integer | ||
if (arg.$position < 0) throw (0, _MinimongoError2.default)("$position in $push must be zero or positive", { field: field }); | ||
if (arg.$position < 0) throw MinimongoError("$position in $push must be zero or positive", { field: field }); | ||
position = arg.$position; | ||
@@ -312,5 +286,5 @@ } | ||
if ('$slice' in arg) { | ||
if (typeof arg.$slice !== "number") throw (0, _MinimongoError2.default)("$slice must be a numeric value", { field: field }); | ||
if (typeof arg.$slice !== "number") throw MinimongoError("$slice must be a numeric value", { field: field }); | ||
// XXX should check to make sure integer | ||
if (arg.$slice > 0) throw (0, _MinimongoError2.default)("$slice in $push must be zero or negative", { field: field }); | ||
if (arg.$slice > 0) throw MinimongoError("$slice in $push must be zero or negative", { field: field }); | ||
slice = arg.$slice; | ||
@@ -322,3 +296,3 @@ } | ||
if (arg.$sort) { | ||
if (slice === undefined) throw (0, _MinimongoError2.default)("$sort requires $slice to be present", { field: field }); | ||
if (slice === undefined) throw MinimongoError("$sort requires $slice to be present", { field: field }); | ||
// XXX this allows us to use a $sort whose value is an array, but that's | ||
@@ -330,4 +304,4 @@ // actually an extension of the Node driver, so it won't work | ||
for (var i = 0; i < toPush.length; i++) { | ||
if (_selector._f._type(toPush[i]) !== 3) { | ||
throw (0, _MinimongoError2.default)("$push like modifiers using $sort " + "require all elements to be objects", { field: field }); | ||
if (_f._type(toPush[i]) !== 3) { | ||
throw MinimongoError("$push like modifiers using $sort " + "require all elements to be objects", { field: field }); | ||
} | ||
@@ -359,6 +333,6 @@ } | ||
$pushAll: function $pushAll(target, field, arg) { | ||
if (!((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === "object" && arg instanceof Array)) throw (0, _MinimongoError2.default)("Modifier $pushAll/pullAll allowed for arrays only"); | ||
(0, _validation.assertHasValidFieldNames)(arg); | ||
if (!((typeof arg === 'undefined' ? 'undefined' : babelHelpers.typeof(arg)) === "object" && arg instanceof Array)) throw MinimongoError("Modifier $pushAll/pullAll allowed for arrays only"); | ||
assertHasValidFieldNames(arg); | ||
var x = target[field]; | ||
if (x === undefined) target[field] = arg;else if (!(x instanceof Array)) throw (0, _MinimongoError2.default)("Cannot apply $pushAll modifier to non-array", { field: field });else { | ||
if (x === undefined) target[field] = arg;else if (!(x instanceof Array)) throw MinimongoError("Cannot apply $pushAll modifier to non-array", { field: field });else { | ||
for (var i = 0; i < arg.length; i++) { | ||
@@ -371,6 +345,6 @@ x.push(arg[i]); | ||
var isEach = false; | ||
if ((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === "object") { | ||
if ((typeof arg === 'undefined' ? 'undefined' : babelHelpers.typeof(arg)) === "object") { | ||
//check if first key is '$each' | ||
var keys = Object.keys(arg); | ||
if (keys[0] === "$each") { | ||
var _keys = Object.keys(arg); | ||
if (_keys[0] === "$each") { | ||
isEach = true; | ||
@@ -380,8 +354,8 @@ } | ||
var values = isEach ? arg["$each"] : [arg]; | ||
(0, _validation.assertHasValidFieldNames)(values); | ||
assertHasValidFieldNames(values); | ||
var x = target[field]; | ||
if (x === undefined) target[field] = values;else if (!(x instanceof Array)) throw (0, _MinimongoError2.default)("Cannot apply $addToSet modifier to non-array", { field: field });else { | ||
_underscore2.default.each(values, function (value) { | ||
if (x === undefined) target[field] = values;else if (!(x instanceof Array)) throw MinimongoError("Cannot apply $addToSet modifier to non-array", { field: field });else { | ||
_.each(values, function (value) { | ||
for (var i = 0; i < x.length; i++) { | ||
if (_selector._f._equal(value, x[i])) return; | ||
if (_f._equal(value, x[i])) return; | ||
}x.push(value); | ||
@@ -394,3 +368,3 @@ }); | ||
var x = target[field]; | ||
if (x === undefined) return;else if (!(x instanceof Array)) throw (0, _MinimongoError2.default)("Cannot apply $pop modifier to non-array", { field: field });else { | ||
if (x === undefined) return;else if (!(x instanceof Array)) throw MinimongoError("Cannot apply $pop modifier to non-array", { field: field });else { | ||
if (typeof arg === 'number' && arg < 0) x.splice(0, 1);else x.pop(); | ||
@@ -402,5 +376,5 @@ } | ||
var x = target[field]; | ||
if (x === undefined) return;else if (!(x instanceof Array)) throw (0, _MinimongoError2.default)("Cannot apply $pull/pullAll modifier to non-array", { field: field });else { | ||
if (x === undefined) return;else if (!(x instanceof Array)) throw MinimongoError("Cannot apply $pull/pullAll modifier to non-array", { field: field });else { | ||
var out = []; | ||
if (arg != null && (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === "object" && !(arg instanceof Array)) { | ||
if (arg != null && (typeof arg === 'undefined' ? 'undefined' : babelHelpers.typeof(arg)) === "object" && !(arg instanceof Array)) { | ||
// XXX would be much nicer to compile this once, rather than | ||
@@ -421,3 +395,3 @@ // for each document we modify.. but usually we're not | ||
for (var i = 0; i < x.length; i++) { | ||
if (!_selector._f._equal(x[i], arg)) out.push(x[i]); | ||
if (!_f._equal(x[i], arg)) out.push(x[i]); | ||
} | ||
@@ -429,6 +403,6 @@ } | ||
$pullAll: function $pullAll(target, field, arg) { | ||
if (!((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === "object" && arg instanceof Array)) throw (0, _MinimongoError2.default)("Modifier $pushAll/pullAll allowed for arrays only", { field: field }); | ||
if (!((typeof arg === 'undefined' ? 'undefined' : babelHelpers.typeof(arg)) === "object" && arg instanceof Array)) throw MinimongoError("Modifier $pushAll/pullAll allowed for arrays only", { field: field }); | ||
if (target === undefined) return; | ||
var x = target[field]; | ||
if (x === undefined) return;else if (!(x instanceof Array)) throw (0, _MinimongoError2.default)("Cannot apply $pull/pullAll modifier to non-array", { field: field });else { | ||
if (x === undefined) return;else if (!(x instanceof Array)) throw MinimongoError("Cannot apply $pull/pullAll modifier to non-array", { field: field });else { | ||
var out = []; | ||
@@ -438,3 +412,3 @@ for (var i = 0; i < x.length; i++) { | ||
for (var j = 0; j < arg.length; j++) { | ||
if (_selector._f._equal(x[i], arg[j])) { | ||
if (_f._equal(x[i], arg[j])) { | ||
exclude = true; | ||
@@ -452,9 +426,9 @@ break; | ||
// no idea why mongo has this restriction.. | ||
throw (0, _MinimongoError2.default)("$rename source must differ from target", { field: field }); | ||
if (target === null) throw (0, _MinimongoError2.default)("$rename source field invalid", { field: field }); | ||
if (typeof arg !== "string") throw (0, _MinimongoError2.default)("$rename target must be a string", { field: field }); | ||
throw MinimongoError("$rename source must differ from target", { field: field }); | ||
if (target === null) throw MinimongoError("$rename source field invalid", { field: field }); | ||
if (typeof arg !== "string") throw MinimongoError("$rename target must be a string", { field: field }); | ||
if (arg.indexOf('\0') > -1) { | ||
// Null bytes are not allowed in Mongo field names | ||
// https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names | ||
throw (0, _MinimongoError2.default)("The 'to' field for $rename cannot contain an embedded null byte", { field: field }); | ||
throw MinimongoError("The 'to' field for $rename cannot contain an embedded null byte", { field: field }); | ||
} | ||
@@ -467,3 +441,3 @@ if (target === undefined) return; | ||
var target2 = findModTarget(doc, keyparts, { forbidArray: true }); | ||
if (target2 === null) throw (0, _MinimongoError2.default)("$rename target field invalid", { field: field }); | ||
if (target2 === null) throw MinimongoError("$rename target field invalid", { field: field }); | ||
var field2 = keyparts.pop(); | ||
@@ -475,4 +449,4 @@ target2[field2] = v; | ||
// native javascript numbers (doubles) so far, so we can't support $bit | ||
throw (0, _MinimongoError2.default)("$bit is not supported", { field: field }); | ||
throw MinimongoError("$bit is not supported", { field: field }); | ||
} | ||
}; |
@@ -1,13 +0,4 @@ | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
import update from 'immutability-helper'; | ||
import modify from './modify.js'; | ||
var _immutabilityHelper = require('immutability-helper'); | ||
var _immutabilityHelper2 = _interopRequireDefault(_immutabilityHelper); | ||
var _modify = require('./modify.js'); | ||
var _modify2 = _interopRequireDefault(_modify); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Examples are taken from https://docs.mongodb.com/manual/reference/ | ||
@@ -19,3 +10,3 @@ describe("modify", function () { | ||
var updatedObject = (0, _modify2.default)(myObject, { $currentDate: "lastModified" }); | ||
var updatedObject = modify(myObject, { $currentDate: "lastModified" }); | ||
@@ -30,5 +21,5 @@ var expectedObject = { existingItem: "here", lastModified: new Date() }; // placeholder, obviously wrong | ||
var updatedObject = (0, _modify2.default)(myObject, { $min: { lowScore: 150 } }); | ||
var updatedObject = modify(myObject, { $min: { lowScore: 150 } }); | ||
var expectedObject = _extends({}, myObject, { lowScore: 150 }); | ||
var expectedObject = babelHelpers.extends({}, myObject, { lowScore: 150 }); | ||
expect(updatedObject).toEqual(expectedObject); | ||
@@ -39,5 +30,5 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { $min: { lowScore: 250 } }); | ||
var updatedObject = modify(myObject, { $min: { lowScore: 250 } }); | ||
var expectedObject = _extends({}, myObject); | ||
var expectedObject = babelHelpers.extends({}, myObject); | ||
expect(updatedObject).toEqual(expectedObject); | ||
@@ -50,5 +41,5 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { $max: { highScore: 950 } }); | ||
var updatedObject = modify(myObject, { $max: { highScore: 950 } }); | ||
var expectedObject = _extends({}, myObject, { highScore: 950 }); | ||
var expectedObject = babelHelpers.extends({}, myObject, { highScore: 950 }); | ||
expect(updatedObject).toEqual(expectedObject); | ||
@@ -59,5 +50,5 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { $max: { highScore: 870 } }); | ||
var updatedObject = modify(myObject, { $max: { highScore: 870 } }); | ||
var expectedObject = _extends({}, myObject); | ||
var expectedObject = babelHelpers.extends({}, myObject); | ||
expect(updatedObject).toEqual(expectedObject); | ||
@@ -78,5 +69,5 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { $inc: { quantity: -2, "metrics.orders": 1 } }); | ||
var updatedObject = modify(myObject, { $inc: { quantity: -2, "metrics.orders": 1 } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
quantity: { $set: myObject.quantity - 2 }, | ||
@@ -105,3 +96,3 @@ metrics: { | ||
var updatedObject = (0, _modify2.default)(myObject, { $set: { | ||
var updatedObject = modify(myObject, { $set: { | ||
quantity: 500, | ||
@@ -113,3 +104,3 @@ details: { model: "14Q3", make: "xyz" }, | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
quantity: { $set: 500 }, | ||
@@ -135,5 +126,5 @@ details: { $set: { model: "14Q3", make: "xyz" } }, | ||
var updatedObject = (0, _modify2.default)(myObject, { $set: { "details.make": "zzz" } }); | ||
var updatedObject = modify(myObject, { $set: { "details.make": "zzz" } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
details: { | ||
@@ -159,3 +150,3 @@ make: { $set: "zzz" } | ||
var updatedObject = (0, _modify2.default)(myObject, { $set: { | ||
var updatedObject = modify(myObject, { $set: { | ||
"tags.1": "rain gear", | ||
@@ -166,3 +157,3 @@ "ratings.0.rating": 2 | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
tags: { | ||
@@ -186,5 +177,5 @@ 1: { $set: "rain gear" } | ||
var updatedObject = (0, _modify2.default)(myObject, { $unset: { quantity: "", instock: "" } }); | ||
var updatedObject = modify(myObject, { $unset: { quantity: "", instock: "" } }); | ||
var expectedObject = _extends({}, myObject); | ||
var expectedObject = babelHelpers.extends({}, myObject); | ||
delete expectedObject.quantity; | ||
@@ -200,5 +191,5 @@ delete expectedObject.instock; | ||
var updatedObject = (0, _modify2.default)(myObject, { $push: { scores: 89 } }); | ||
var updatedObject = modify(myObject, { $push: { scores: 89 } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
scores: { $push: [89] } | ||
@@ -212,5 +203,5 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { $push: { scores: { $each: [90, 92, 85] } } }); | ||
var updatedObject = modify(myObject, { $push: { scores: { $each: [90, 92, 85] } } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
scores: { $push: [90, 92, 85] } | ||
@@ -231,3 +222,3 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { | ||
var updatedObject = modify(myObject, { | ||
$push: { | ||
@@ -258,5 +249,5 @@ quizzes: { | ||
var updatedObject = (0, _modify2.default)(myObject, { $pushAll: { scores: [90, 92, 85] } }); | ||
var updatedObject = modify(myObject, { $pushAll: { scores: [90, 92, 85] } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { | ||
var expectedObject = update(myObject, { | ||
scores: { $push: [90, 92, 85] } | ||
@@ -273,3 +264,3 @@ }); | ||
var updatedObject = (0, _modify2.default)(myObject, { $addToSet: { letters: ["c", "d"] } }); | ||
var updatedObject = modify(myObject, { $addToSet: { letters: ["c", "d"] } }); | ||
@@ -284,5 +275,5 @@ var expectedObject = { _id: 1, letters: ["a", "b", ["c", "d"]] }; | ||
var updatedObject = (0, _modify2.default)(myObject, { $addToSet: { tags: "accessories" } }); | ||
var updatedObject = modify(myObject, { $addToSet: { tags: "accessories" } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { tags: { $push: ["accessories"] } }); | ||
var expectedObject = update(myObject, { tags: { $push: ["accessories"] } }); | ||
@@ -294,5 +285,5 @@ expect(updatedObject).toEqual(expectedObject); | ||
var updatedObject = (0, _modify2.default)(myObject, { $addToSet: { tags: "camera" } }); | ||
var updatedObject = modify(myObject, { $addToSet: { tags: "camera" } }); | ||
var expectedObject = _extends({}, myObject); | ||
var expectedObject = babelHelpers.extends({}, myObject); | ||
@@ -305,5 +296,5 @@ expect(updatedObject).toEqual(expectedObject); | ||
var updatedObject = (0, _modify2.default)(myObject, { $addToSet: { tags: { $each: ["camera", "electronics", "accessories"] } } }); | ||
var updatedObject = modify(myObject, { $addToSet: { tags: { $each: ["camera", "electronics", "accessories"] } } }); | ||
var expectedObject = (0, _immutabilityHelper2.default)(myObject, { tags: { $push: ["camera", "accessories"] } }); | ||
var expectedObject = update(myObject, { tags: { $push: ["camera", "accessories"] } }); | ||
@@ -318,3 +309,3 @@ expect(updatedObject).toEqual(expectedObject); | ||
var updatedObject = (0, _modify2.default)(myObject, { $pop: { scores: -1 } }); | ||
var updatedObject = modify(myObject, { $pop: { scores: -1 } }); | ||
@@ -328,3 +319,3 @@ var expectedObject = { _id: 1, scores: [9, 10] }; | ||
var updatedObject = (0, _modify2.default)(myObject, { $pop: { scores: 1 } }); | ||
var updatedObject = modify(myObject, { $pop: { scores: 1 } }); | ||
@@ -344,3 +335,3 @@ var expectedObject = { _id: 1, scores: [9] }; | ||
var updatedObject = (0, _modify2.default)(myObject, { $pull: { fruits: { $in: ["apples", "oranges"] }, vegetables: "carrots" } }); | ||
var updatedObject = modify(myObject, { $pull: { fruits: { $in: ["apples", "oranges"] }, vegetables: "carrots" } }); | ||
@@ -359,3 +350,3 @@ var expectedObject = { | ||
var updatedObject = (0, _modify2.default)(myObject, { $pull: { votes: { $gte: 6 } } }); | ||
var updatedObject = modify(myObject, { $pull: { votes: { $gte: 6 } } }); | ||
@@ -372,3 +363,3 @@ var expectedObject = { _id: 1, votes: [3, 5] }; | ||
var updatedObject = (0, _modify2.default)(myObject, { $pullAll: { scores: [0, 5] } }); | ||
var updatedObject = modify(myObject, { $pullAll: { scores: [0, 5] } }); | ||
@@ -390,7 +381,7 @@ var expectedObject = { _id: 1, scores: [2, 1] }; | ||
var updatedObject = (0, _modify2.default)(myObject, { $rename: { "nmae": "name" } }); | ||
var updatedObject = modify(myObject, { $rename: { "nmae": "name" } }); | ||
var expectedObject = _extends({}, myObject); | ||
var expectedObject = babelHelpers.extends({}, myObject); | ||
delete expectedObject.nmae; | ||
expectedObject.name = _extends({}, myObject.nmae); | ||
expectedObject.name = babelHelpers.extends({}, myObject.nmae); | ||
@@ -400,2 +391,9 @@ expect(updatedObject).toEqual(expectedObject); | ||
}); | ||
it("throws an error when the operand path contains an empty field name", function () { | ||
var myObject = {}; | ||
expect(function () { | ||
modify(myObject, { $set: { "test.abc.": "name" } }); | ||
}).toThrow(/empty field name/); | ||
}); | ||
}); |
@@ -1,13 +0,5 @@ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports._f = undefined; | ||
import { isArray } from './helpers'; | ||
import { EJSON } from './ejson'; | ||
var _helpers = require('./helpers'); | ||
var _ejson = require('./ejson'); | ||
var _id = require('./id'); | ||
var _f = exports._f = { | ||
export var _f = { | ||
// XXX for _all and _in, consider building 'inquery' at compile time.. | ||
@@ -19,3 +11,3 @@ | ||
if (typeof v === "boolean") return 8; | ||
if ((0, _helpers.isArray)(v)) return 4; | ||
if (isArray(v)) return 4; | ||
if (v === null) return 10; | ||
@@ -27,4 +19,3 @@ if (v instanceof RegExp) | ||
if (v instanceof Date) return 9; | ||
if (_ejson.EJSON.isBinary(v)) return 5; | ||
if (v instanceof _id.MongoID.ObjectID) return 7; | ||
if (EJSON.isBinary(v)) return 5; | ||
return 3; // object | ||
@@ -43,3 +34,3 @@ | ||
_equal: function _equal(a, b) { | ||
return _ejson.EJSON.equals(a, b, { keyOrderSensitive: true }); | ||
return EJSON.equals(a, b, { keyOrderSensitive: true }); | ||
}, | ||
@@ -46,0 +37,0 @@ |
@@ -1,20 +0,4 @@ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
import { isString } from 'underscore'; | ||
import MinimongoError from './MinimongoError'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
exports.assertIsValidFieldName = assertIsValidFieldName; | ||
exports.assertHasValidFieldNames = assertHasValidFieldNames; | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
var _MinimongoError = require('./MinimongoError'); | ||
var _MinimongoError2 = _interopRequireDefault(_MinimongoError); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// Make sure field names do not contain Mongo restricted | ||
@@ -28,6 +12,6 @@ // characters ('.', '$', '\0'). | ||
}; | ||
function assertIsValidFieldName(key) { | ||
export function assertIsValidFieldName(key) { | ||
var match = void 0; | ||
if (_underscore2.default.isString(key) && (match = key.match(/^\$|\.|\0/))) { | ||
throw (0, _MinimongoError2.default)('Key ' + key + ' must not ' + invalidCharMsg[match[0]]); | ||
if (isString(key) && (match = key.match(/^\$|\.|\0/))) { | ||
throw MinimongoError('Key ' + key + ' must not ' + invalidCharMsg[match[0]]); | ||
} | ||
@@ -37,4 +21,4 @@ }; | ||
// checks if all field names in an object are valid | ||
function assertHasValidFieldNames(doc) { | ||
if (doc && (typeof doc === 'undefined' ? 'undefined' : _typeof(doc)) === "object") { | ||
export function assertHasValidFieldNames(doc) { | ||
if (doc && (typeof doc === 'undefined' ? 'undefined' : babelHelpers.typeof(doc)) === "object") { | ||
JSON.stringify(doc, function (key, value) { | ||
@@ -41,0 +25,0 @@ assertIsValidFieldName(key); |
{ | ||
"name": "modifyjs", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Modify your objects with a mongo syntax.", | ||
"main": "dist/modify.js", | ||
"main": "dist/bundle.js", | ||
"scripts": { | ||
"test": "jest", | ||
"build": "babel src --out-dir dist" | ||
"test": "BABEL_ENV=dev jest src/", | ||
"build": "BABEL_ENV=rollup rollup src/modify.js --config rollup.config.prod.js", | ||
"browserify": "npm run build ; browserify dist/modify.js -o dist/bundle.js --full-paths", | ||
"weight": "npm run browserify ; cat dist/bundle.js | uglifyjs --compress --mangle | discify --open --full-paths" | ||
}, | ||
@@ -29,10 +31,20 @@ "repository": { | ||
"babel-jest": "^19.0.0", | ||
"babel-preset-es2015-without-strict": "0.0.4", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-latest": "^6.24.1", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"browserify": "^14.3.0", | ||
"disc": "^1.3.2", | ||
"immutability-helper": "^2.2.0", | ||
"jest": "^19.0.2" | ||
"jest": "^19.0.2", | ||
"rollup": "^0.41.6", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"rollup-plugin-visualizer": "^0.2.1", | ||
"uglifyjs": "^2.4.10" | ||
}, | ||
"dependencies": { | ||
"underscore": "^1.8.3" | ||
"clone": "^2.1.1", | ||
"deep-equal": "^1.0.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
245580
27
2583
2
15
2
+ Addedclone@^2.1.1
+ Addeddeep-equal@^1.0.1
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.2(transitive)
+ Addedclone@2.1.2(transitive)
+ Addeddeep-equal@1.1.2(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddunder-proto@1.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.6(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-arguments@1.2.0(transitive)
+ Addedis-date-object@1.1.0(transitive)
+ Addedis-regex@1.2.1(transitive)
+ Addedmath-intrinsics@1.0.0(transitive)
+ Addedobject-is@1.1.6(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedregexp.prototype.flags@1.5.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
- Removedunderscore@^1.8.3
- Removedunderscore@1.13.7(transitive)