ampersand-state
Advanced tools
Comparing version 4.6.0 to 4.7.0
@@ -0,23 +1,18 @@ | ||
'use strict'; | ||
/*$AMPERSAND_VERSION*/ | ||
var uniqueId = require('lodash.uniqueid'); | ||
var assign = require('lodash.assign'); | ||
var cloneObj = function(obj) { return assign({}, obj); }; | ||
var omit = require('lodash.omit'); | ||
var escape = require('lodash.escape'); | ||
var forEach = require('lodash.foreach'); | ||
var forOwn = require('lodash.forown'); | ||
var includes = require('lodash.includes'); | ||
var isString = require('lodash.isstring'); | ||
var isObject = require('lodash.isobject'); | ||
var isArray = require('lodash.isarray'); | ||
var isDate = require('lodash.isdate'); | ||
var isUndefined = require('lodash.isundefined'); | ||
var isFunction = require('lodash.isfunction'); | ||
var isNull = require('lodash.isnull'); | ||
var isEmpty = require('lodash.isempty'); | ||
var isEqual = require('lodash.isequal'); | ||
var clone = require('lodash.clone'); | ||
var _isEqual = require('lodash.isequal'); // to avoid shadowing | ||
var has = require('lodash.has'); | ||
var result = require('lodash.result'); | ||
var keys = require('lodash.keys'); | ||
var bind = require('lodash.bind'); | ||
var defaults = require('lodash.defaults'); | ||
var bind = require('lodash.bind'); // because phantomjs doesn't have Function#bind | ||
var union = require('lodash.union'); | ||
@@ -109,6 +104,6 @@ var Events = require('ampersand-events'); | ||
var res = this.getAttributes(attrOpts, true); | ||
forEach(this._children, function (value, key) { | ||
forOwn(this._children, function (value, key) { | ||
res[key] = this[key].serialize(); | ||
}, this); | ||
forEach(this._collections, function (value, key) { | ||
forOwn(this._collections, function (value, key) { | ||
res[key] = this[key].serialize(); | ||
@@ -201,9 +196,9 @@ }, this); | ||
if (isUndefined(newVal) && def.required) { | ||
if (newVal === undefined && def.required) { | ||
throw new TypeError('Required property \'' + attr + '\' must be of type ' + def.type + '. Tried to set ' + newVal); | ||
} | ||
if (isNull(newVal) && def.required && !def.allowNull) { | ||
if (newVal === null && def.required && !def.allowNull) { | ||
throw new TypeError('Property \'' + attr + '\' must be of type ' + def.type + ' (cannot be null). Tried to set ' + newVal); | ||
} | ||
if ((def.type && def.type !== 'any' && def.type !== newType) && !isNull(newVal) && !isUndefined(newVal)) { | ||
if ((def.type && def.type !== 'any' && def.type !== newType) && newVal !== null && newVal !== undefined) { | ||
throw new TypeError('Property \'' + attr + '\' must be of type ' + def.type + '. Tried to set ' + newVal); | ||
@@ -233,3 +228,3 @@ } | ||
// actually update our values | ||
forEach(changes, function (change) { | ||
changes.forEach(function (change) { | ||
self._previousAttributes[change.key] = change.prev; | ||
@@ -245,3 +240,3 @@ if (unset) { | ||
if (!silent) { | ||
forEach(changes, function (change) { | ||
changes.forEach(function (change) { | ||
self.trigger('change:' + change.key, self, change.val, options); | ||
@@ -289,3 +284,3 @@ }); | ||
previousAttributes: function () { | ||
return clone(this._previousAttributes); | ||
return cloneObj(this._previousAttributes); | ||
}, | ||
@@ -296,3 +291,3 @@ | ||
hasChanged: function (attr) { | ||
if (attr == null) return !isEmpty(this._changed); | ||
if (attr == null) return !!Object.keys(this._changed).length; | ||
return has(this._changed, attr); | ||
@@ -308,3 +303,3 @@ }, | ||
changedAttributes: function (diff) { | ||
if (!diff) return this.hasChanged() ? clone(this._changed) : false; | ||
if (!diff) return this.hasChanged() ? cloneObj(this._changed) : false; | ||
var val, changed = false; | ||
@@ -328,13 +323,14 @@ var old = this._changing ? this._previousAttributes : this.attributes; | ||
unset: function (attrs, options) { | ||
var self = this; | ||
attrs = Array.isArray(attrs) ? attrs : [attrs]; | ||
forEach(attrs, function (key) { | ||
var def = this._definition[key]; | ||
attrs.forEach(function (key) { | ||
var def = self._definition[key]; | ||
var val; | ||
if (def.required) { | ||
val = result(def, 'default'); | ||
return this.set(key, val, options); | ||
return self.set(key, val, options); | ||
} else { | ||
return this.set(key, val, assign({}, options, {unset: true})); | ||
return self.set(key, val, assign({}, options, {unset: true})); | ||
} | ||
}, this); | ||
}); | ||
}, | ||
@@ -344,3 +340,3 @@ | ||
var self = this; | ||
forEach(keys(this.attributes), function (key) { | ||
Object.keys(this.attributes).forEach(function (key) { | ||
self.unset(key, options); | ||
@@ -366,3 +362,3 @@ }); | ||
if (dataType && dataType.compare) return bind(dataType.compare, this); | ||
return isEqual; | ||
return _isEqual; // if no compare function is defined, use _.isEqual | ||
}, | ||
@@ -388,12 +384,12 @@ | ||
_ensureValidType: function (type) { | ||
return includes(['string', 'number', 'boolean', 'array', 'object', 'date', 'any'].concat(keys(this._dataTypes)), type) ? type : undefined; | ||
return includes(['string', 'number', 'boolean', 'array', 'object', 'date', 'any'] | ||
.concat(Object.keys(this._dataTypes)), type) ? type : undefined; | ||
}, | ||
getAttributes: function (options, raw) { | ||
options || (options = {}); | ||
defaults(options, { | ||
options = assign({ | ||
session: false, | ||
props: false, | ||
derived: false | ||
}); | ||
}, options || {}); | ||
var res = {}; | ||
@@ -404,3 +400,4 @@ var val, item, def; | ||
if ((options.session && def.session) || (options.props && !def.session)) { | ||
val = (raw) ? this._values[item] : this[item]; | ||
val = raw ? this._values[item] : this[item]; | ||
if (raw && val && isFunction(val.serialize)) val = val.serialize(); | ||
if (typeof val === 'undefined') val = result(def, 'default'); | ||
@@ -419,3 +416,3 @@ if (typeof val !== 'undefined') res[item] = val; | ||
forEach(this._derived, function (value, name) { | ||
forOwn(this._derived, function (value, name) { | ||
var def = self._derived[name]; | ||
@@ -550,3 +547,3 @@ def.deps = def.depList; | ||
//Transform array of ['type', required, default] to object form | ||
if (isArray(desc)) { | ||
if (Array.isArray(desc)) { | ||
descArray = desc; | ||
@@ -573,3 +570,3 @@ desc = { | ||
if (desc.setOnce) def.setOnce = true; | ||
if (def.required && isUndefined(def['default']) && !def.setOnce) def['default'] = object._getDefaultForType(type); | ||
if (def.required && def['default'] === undefined && !def.setOnce) def['default'] = object._getDefaultForType(type); | ||
def.test = desc.test; | ||
@@ -616,3 +613,3 @@ def.values = desc.values; | ||
// add to our shared dependency list | ||
forEach(def.depList, function (dep) { | ||
def.depList.forEach(function (dep) { | ||
modelProto._deps[dep] = union(modelProto._deps[dep] || [], [name]); | ||
@@ -644,12 +641,12 @@ }); | ||
} else if (!isDate(newVal)) { | ||
try { | ||
var dateVal = new Date(newVal).valueOf(); | ||
if (isNaN(dateVal)) { | ||
// If the newVal cant be parsed, then try parseInt first | ||
dateVal = new Date(parseInt(newVal, 10)).valueOf(); | ||
if (isNaN(dateVal)) throw TypeError; | ||
} | ||
newVal = dateVal; | ||
newType = 'date'; | ||
} catch (e) { | ||
var err = null; | ||
var dateVal = new Date(newVal).valueOf(); | ||
if (isNaN(dateVal)) { | ||
// If the newVal cant be parsed, then try parseInt first | ||
dateVal = new Date(parseInt(newVal, 10)).valueOf(); | ||
if (isNaN(dateVal)) err = true; | ||
} | ||
newVal = dateVal; | ||
newType = 'date'; | ||
if (err) { | ||
newType = typeof newVal; | ||
@@ -679,3 +676,3 @@ } | ||
val: newVal, | ||
type: isArray(newVal) ? 'array' : typeof newVal | ||
type: Array.isArray(newVal) ? 'array' : typeof newVal | ||
}; | ||
@@ -693,3 +690,3 @@ }, | ||
// should work too, IMO. We just override it, in that case. | ||
if (newType !== 'object' && isUndefined(newVal)) { | ||
if (newType !== 'object' && newVal === undefined) { | ||
newVal = null; | ||
@@ -747,5 +744,5 @@ newType = 'object'; | ||
function extend(protoProps) { | ||
/*jshint validthis:true*/ | ||
var parent = this; | ||
var child; | ||
var args = [].slice.call(arguments); | ||
@@ -785,5 +782,6 @@ // The constructor function for the new subclass is either defined by you | ||
]; | ||
args.forEach(function processArg(def) { | ||
for(var i = 0; i < arguments.length; i++) { | ||
var def = arguments[i]; | ||
if (def.dataTypes) { | ||
forEach(def.dataTypes, function (def, name) { | ||
forOwn(def.dataTypes, function (def, name) { | ||
child.prototype._dataTypes[name] = def; | ||
@@ -793,3 +791,3 @@ }); | ||
if (def.props) { | ||
forEach(def.props, function (def, name) { | ||
forOwn(def.props, function (def, name) { | ||
createPropertyDefinition(child.prototype, name, def); | ||
@@ -799,3 +797,3 @@ }); | ||
if (def.session) { | ||
forEach(def.session, function (def, name) { | ||
forOwn(def.session, function (def, name) { | ||
createPropertyDefinition(child.prototype, name, def, true); | ||
@@ -805,3 +803,3 @@ }); | ||
if (def.derived) { | ||
forEach(def.derived, function (def, name) { | ||
forOwn(def.derived, function (def, name) { | ||
createDerivedProperty(child.prototype, name, def); | ||
@@ -811,3 +809,3 @@ }); | ||
if (def.collections) { | ||
forEach(def.collections, function (constructor, name) { | ||
forOwn(def.collections, function (constructor, name) { | ||
child.prototype._collections[name] = constructor; | ||
@@ -817,3 +815,3 @@ }); | ||
if (def.children) { | ||
forEach(def.children, function (constructor, name) { | ||
forOwn(def.children, function (constructor, name) { | ||
child.prototype._children[name] = constructor; | ||
@@ -823,3 +821,3 @@ }); | ||
assign(child.prototype, omit(def, omitFromExtend)); | ||
}); | ||
} | ||
} | ||
@@ -826,0 +824,0 @@ |
{ | ||
"name": "ampersand-state", | ||
"description": "An observable, extensible state object with derived watchable properties.", | ||
"version": "4.6.0", | ||
"version": "4.7.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -22,8 +22,6 @@ "files": [ | ||
"key-tree-store": "^1.3.0", | ||
"lodash.assign": "^3.0.0", | ||
"lodash.assign": "^3.2.0", | ||
"lodash.bind": "^3.1.0", | ||
"lodash.clone": "^3.0.3", | ||
"lodash.defaults": "^3.1.2", | ||
"lodash.escape": "^3.0.0", | ||
"lodash.foreach": "^3.0.2", | ||
"lodash.forown": "^3.0.2", | ||
"lodash.has": "^3.0.0", | ||
@@ -33,10 +31,6 @@ "lodash.includes": "^3.1.3", | ||
"lodash.isdate": "^3.0.1", | ||
"lodash.isempty": "^3.0.1", | ||
"lodash.isequal": "^3.0.1", | ||
"lodash.isfunction": "^3.0.6", | ||
"lodash.isnull": "^3.0.0", | ||
"lodash.isobject": "^3.0.1", | ||
"lodash.isstring": "^3.0.1", | ||
"lodash.isundefined": "^3.0.0", | ||
"lodash.keys": "^3.1.2", | ||
"lodash.omit": "^3.1.0", | ||
@@ -51,2 +45,4 @@ "lodash.result": "^3.0.0", | ||
"browserify": "^11.0.1", | ||
"coveralls": "^2.11.4", | ||
"istanbul": "^0.4.0", | ||
"jshint": "^2.5.3", | ||
@@ -74,9 +70,12 @@ "phantomjs": "^1.9.7-15", | ||
"test": "browserify test/index.js | tape-run | tap-spec", | ||
"coverage": "rm -rf coverage && istanbul cover -- tape test/index.js --verbose", | ||
"validate": "npm ls", | ||
"start": "run-browser test/index.js", | ||
"lint": "jshint .", | ||
"lint": "jshint ampersand-state.js ./test/*", | ||
"benchmark": "node --allow-natives-syntax benchmark/massCreate.js", | ||
"preversion": "git checkout master && git pull && npm ls", | ||
"publish-patch": "npm run preversion && npm version patch && git push origin master --tags && npm publish", | ||
"publish-minor": "npm run preversion && npm version minor && git push origin master --tags && npm publish", | ||
"publish-major": "npm run preversion && npm version major && git push origin master --tags && npm publish" | ||
"publish-major": "npm run preversion && npm version major && git push origin master --tags && npm publish", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls" | ||
}, | ||
@@ -83,0 +82,0 @@ "testling": { |
# ampersand-state | ||
[![Coverage Status](https://coveralls.io/repos/AmpersandJS/ampersand-state/badge.svg?branch=master&service=github)](https://coveralls.io/github/AmpersandJS/ampersand-state?branch=master) | ||
<!-- starthide --> | ||
@@ -71,3 +73,3 @@ Part of the [Ampersand.js toolkit](http://ampersandjs.com) for building clientside applications. | ||
var me = new Person({ | ||
firstName: 'Phil' | ||
firstName: 'Phil', | ||
lastName: 'Roberts' | ||
@@ -74,0 +76,0 @@ }); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
61581
20
710
12
717
+ Addedlodash.forown@^3.0.2
+ Addedlodash.forown@3.0.2(transitive)
- Removedlodash.clone@^3.0.3
- Removedlodash.defaults@^3.1.2
- Removedlodash.foreach@^3.0.2
- Removedlodash.isempty@^3.0.1
- Removedlodash.isnull@^3.0.0
- Removedlodash.isundefined@^3.0.0
- Removedlodash.keys@^3.1.2
- Removedlodash._arraycopy@3.0.0(transitive)
- Removedlodash._baseclone@3.3.0(transitive)
- Removedlodash.clone@3.0.3(transitive)
- Removedlodash.defaults@3.1.2(transitive)
- Removedlodash.isnull@3.0.0(transitive)
- Removedlodash.isundefined@3.0.1(transitive)
Updatedlodash.assign@^3.2.0