Socket
Socket
Sign inDemoInstall

ampersand-state

Package Overview
Dependencies
Maintainers
5
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ampersand-state - npm Package Compare versions

Comparing version 4.4.5 to 4.4.6

143

ampersand-state.js
/*$AMPERSAND_VERSION*/
var _ = require('underscore');
var uniqueId = require("lodash.uniqueid");
var assign = require("lodash.assign");
var omit = require("lodash.omit");
var contains = require("lodash.contains");
var escape = require("lodash.escape");
var forEach = require("lodash.foreach");
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 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 union = require("lodash.union");
var BBEvents = require('backbone-events-standalone');

@@ -10,3 +31,3 @@ var KeyTree = require('key-tree-store');

options || (options = {});
this.cid || (this.cid = _.uniqueId('state'));
this.cid || (this.cid = uniqueId('state'));
this._events = {};

@@ -23,3 +44,3 @@ this._values = {};

this._previousAttributes = {};
if (attrs) this.set(attrs, _.extend({silent: true, initial: true}, options));
if (attrs) this.set(attrs, assign({silent: true, initial: true}, options));
this._changed = {};

@@ -31,3 +52,3 @@ if (this._derived) this._initDerived();

_.extend(Base.prototype, BBEvents, {
assign(Base.prototype, BBEvents, {
// can be allow, ignore, reject

@@ -72,3 +93,3 @@ extraProperties: 'ignore',

escape: function (attr) {
return _.escape(this.get(attr));
return escape(this.get(attr));
},

@@ -78,3 +99,3 @@

isValid: function (options) {
return this._validate({}, _.extend(options || {}, { validate: true }));
return this._validate({}, assign(options || {}, { validate: true }));
},

@@ -92,6 +113,6 @@

var res = this.getAttributes({props: true}, true);
_.each(this._children, function (value, key) {
forEach(this._children, function (value, key) {
res[key] = this[key].serialize();
}, this);
_.each(this._collections, function (value, key) {
forEach(this._collections, function (value, key) {
res[key] = this[key].serialize();

@@ -113,3 +134,3 @@ }, this);

// Handle both `"key", value` and `{key: value}` -style arguments.
if (_.isObject(key) || key === null) {
if (isObject(key) || key === null) {
attrs = key;

@@ -187,12 +208,12 @@ options = value;

if (_.isUndefined(newVal) && def.required) {
if (isUndefined(newVal) && 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 (isNull(newVal) && 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) && !isNull(newVal) && !isUndefined(newVal)) {
throw new TypeError('Property \'' + attr + '\' must be of type ' + def.type + '. Tried to set ' + newVal);
}
if (def.values && !_.contains(def.values, newVal)) {
if (def.values && !contains(def.values, newVal)) {
throw new TypeError('Property \'' + attr + '\' must be one of values: ' + def.values.join(', ') + '. Tried to set ' + newVal);

@@ -219,3 +240,3 @@ }

// actually update our values
_.each(changes, function (change) {
forEach(changes, function (change) {
self._previousAttributes[change.key] = change.prev;

@@ -231,3 +252,3 @@ if (unset) {

if (!silent) {
_.each(changes, function (change) {
forEach(changes, function (change) {
self.trigger('change:' + change.key, self, change.val, options);

@@ -275,3 +296,3 @@ });

previousAttributes: function () {
return _.clone(this._previousAttributes);
return clone(this._previousAttributes);
},

@@ -282,4 +303,4 @@

hasChanged: function (attr) {
if (attr == null) return !_.isEmpty(this._changed);
return _.has(this._changed, attr);
if (attr == null) return !isEmpty(this._changed);
return has(this._changed, attr);
},

@@ -294,3 +315,3 @@

changedAttributes: function (diff) {
if (!diff) return this.hasChanged() ? _.clone(this._changed) : false;
if (!diff) return this.hasChanged() ? clone(this._changed) : false;
var val, changed = false;

@@ -318,6 +339,6 @@ var old = this._changing ? this._previousAttributes : this.attributes;

if (def.required) {
val = _.result(def, 'default');
val = result(def, 'default');
return this.set(attr, val, options);
} else {
return this.set(attr, val, _.extend({}, options, {unset: true}));
return this.set(attr, val, assign({}, options, {unset: true}));
}

@@ -328,3 +349,3 @@ },

var self = this;
_.each(_.keys(this.attributes), function (key) {
forEach(keys(this.attributes), function (key) {
self.unset(key, options);

@@ -349,4 +370,4 @@ });

var dataType = this._dataTypes[type];
if (dataType && dataType.compare) return _.bind(dataType.compare, this);
return _.isEqual;
if (dataType && dataType.compare) return bind(dataType.compare, this);
return isEqual;
},

@@ -358,6 +379,6 @@

if (!options.validate || !this.validate) return true;
attrs = _.extend({}, this.attributes, attrs);
attrs = assign({}, this.attributes, attrs);
var error = this.validationError = this.validate(attrs, options) || null;
if (!error) return true;
this.trigger('invalid', this, error, _.extend(options || {}, {validationError: error}));
this.trigger('invalid', this, error, assign(options || {}, {validationError: error}));
return false;

@@ -373,3 +394,3 @@ },

_ensureValidType: function (type) {
return _.contains(['string', 'number', 'boolean', 'array', 'object', 'date', 'any'].concat(_.keys(this._dataTypes)), type) ? type : undefined;
return contains(['string', 'number', 'boolean', 'array', 'object', 'date', 'any'].concat(keys(this._dataTypes)), type) ? type : undefined;
},

@@ -379,3 +400,3 @@

options || (options = {});
_.defaults(options, {
defaults(options, {
session: false,

@@ -391,3 +412,3 @@ props: false,

val = (raw) ? this._values[item] : this[item];
if (typeof val === 'undefined') val = _.result(def, 'default');
if (typeof val === 'undefined') val = result(def, 'default');
if (typeof val !== 'undefined') res[item] = val;

@@ -405,3 +426,3 @@ }

_.each(this._derived, function (value, name) {
forEach(this._derived, function (value, name) {
var def = self._derived[name];

@@ -471,3 +492,3 @@ def.deps = def.depList;

_getEventBubblingHandler: function (propertyName) {
return _.bind(function (name, model, newValue) {
return bind(function (name, model, newValue) {
if (changeRE.test(name)) {

@@ -521,3 +542,3 @@ this.trigger('change:' + propertyName + '.' + name.split(':')[1], model, newValue);

if (_.isString(desc)) {
if (isString(desc)) {
// grab our type if all we've got is a string

@@ -529,3 +550,3 @@ type = object._ensureValidType(desc);

//Transform array of ['type', required, default] to object form
if (_.isArray(desc)) {
if (isArray(desc)) {
descArray = desc;

@@ -551,3 +572,3 @@ desc = {

if (desc.setOnce) def.setOnce = true;
if (def.required && _.isUndefined(def.default) && !def.setOnce) def.default = object._getDefaultForType(type);
if (def.required && isUndefined(def.default) && !def.setOnce) def.default = object._getDefaultForType(type);
def.test = desc.test;

@@ -565,13 +586,13 @@ def.values = desc.values;

get: function () {
var result = this._values[name];
var value = this._values[name];
var typeDef = this._dataTypes[def.type];
if (typeof result !== 'undefined') {
if (typeof value !== 'undefined') {
if (typeDef && typeDef.get) {
result = typeDef.get(result);
value = typeDef.get(value);
}
return result;
return value;
}
result = _.result(def, 'default');
this._values[name] = result;
return result;
value = result(def, 'default');
this._values[name] = value;
return value;
}

@@ -586,3 +607,3 @@ });

var def = modelProto._derived[name] = {
fn: _.isFunction(definition) ? definition : definition.fn,
fn: isFunction(definition) ? definition : definition.fn,
cache: (definition.cache !== false),

@@ -593,4 +614,4 @@ depList: definition.deps || []

// add to our shared dependency list
_.each(def.depList, function (dep) {
modelProto._deps[dep] = _(modelProto._deps[dep] || []).union([name]);
forEach(def.depList, function (dep) {
modelProto._deps[dep] = union(modelProto._deps[dep] || [], [name]);
});

@@ -620,3 +641,3 @@

newType = typeof null;
} else if (!_.isDate(newVal)) {
} else if (!isDate(newVal)) {
try {

@@ -656,3 +677,3 @@ var dateVal = new Date(newVal).valueOf();

val: newVal,
type: _.isArray(newVal) ? 'array' : typeof newVal
type: isArray(newVal) ? 'array' : typeof newVal
};

@@ -670,3 +691,3 @@ },

// should work too, IMO. We just override it, in that case.
if (newType !== 'object' && _.isUndefined(newVal)) {
if (newType !== 'object' && isUndefined(newVal)) {
newVal = null;

@@ -741,3 +762,3 @@ newType = 'object';

// Add static properties to the constructor function from parent
_.extend(child, parent);
assign(child, parent);

@@ -751,8 +772,8 @@ // Set the prototype chain to inherit from `parent`, without calling

// set prototype level objects
child.prototype._derived = _.extend({}, parent.prototype._derived);
child.prototype._deps = _.extend({}, parent.prototype._deps);
child.prototype._definition = _.extend({}, parent.prototype._definition);
child.prototype._collections = _.extend({}, parent.prototype._collections);
child.prototype._children = _.extend({}, parent.prototype._children);
child.prototype._dataTypes = _.extend({}, parent.prototype._dataTypes || dataTypes);
child.prototype._derived = assign({}, parent.prototype._derived);
child.prototype._deps = assign({}, parent.prototype._deps);
child.prototype._definition = assign({}, parent.prototype._definition);
child.prototype._collections = assign({}, parent.prototype._collections);
child.prototype._children = assign({}, parent.prototype._children);
child.prototype._dataTypes = assign({}, parent.prototype._dataTypes || dataTypes);

@@ -766,3 +787,3 @@ // Mix in all prototype properties to the subclass if supplied.

if (def.dataTypes) {
_.each(def.dataTypes, function (def, name) {
forEach(def.dataTypes, function (def, name) {
child.prototype._dataTypes[name] = def;

@@ -772,3 +793,3 @@ });

if (def.props) {
_.each(def.props, function (def, name) {
forEach(def.props, function (def, name) {
createPropertyDefinition(child.prototype, name, def);

@@ -778,3 +799,3 @@ });

if (def.session) {
_.each(def.session, function (def, name) {
forEach(def.session, function (def, name) {
createPropertyDefinition(child.prototype, name, def, true);

@@ -784,3 +805,3 @@ });

if (def.derived) {
_.each(def.derived, function (def, name) {
forEach(def.derived, function (def, name) {
createDerivedProperty(child.prototype, name, def);

@@ -790,3 +811,3 @@ });

if (def.collections) {
_.each(def.collections, function (constructor, name) {
forEach(def.collections, function (constructor, name) {
child.prototype._collections[name] = constructor;

@@ -796,7 +817,7 @@ });

if (def.children) {
_.each(def.children, function (constructor, name) {
forEach(def.children, function (constructor, name) {
child.prototype._children[name] = constructor;
});
}
_.extend(child.prototype, _.omit(def, omitFromExtend));
assign(child.prototype, omit(def, omitFromExtend));
});

@@ -803,0 +824,0 @@ }

{
"name": "ampersand-state",
"description": "An observable, extensible state object with derived watchable properties.",
"version": "4.4.5",
"version": "4.4.6",
"author": "Henrik Joreteg <henrik@andyet.net>",
"files": [
"ampersand-state.js"
],
"browserify": {

@@ -19,3 +22,24 @@ "transform": [

"key-tree-store": "~0.1.0",
"underscore": "^1.6.0"
"lodash.assign": "^3.0.0",
"lodash.bind": "^3.1.0",
"lodash.clone": "^3.0.1",
"lodash.contains": "^2.4.1",
"lodash.defaults": "^3.1.0",
"lodash.escape": "^3.0.0",
"lodash.foreach": "^3.0.2",
"lodash.has": "^3.0.0",
"lodash.isarray": "^3.0.1",
"lodash.isdate": "^3.0.1",
"lodash.isempty": "^3.0.1",
"lodash.isequal": "^3.0.1",
"lodash.isfunction": "^3.0.2",
"lodash.isnull": "^3.0.0",
"lodash.isobject": "^3.0.1",
"lodash.isstring": "^3.0.1",
"lodash.isundefined": "^3.0.0",
"lodash.keys": "^3.0.5",
"lodash.omit": "^3.1.0",
"lodash.result": "^3.0.0",
"lodash.union": "^3.1.0",
"lodash.uniqueid": "^3.0.0"
},

@@ -22,0 +46,0 @@ "devDependencies": {

@@ -115,3 +115,3 @@ # ampersand-state

The property name that should be used as a namespace. Namespaces are completely optional, but exist in case you need to make an additionl distinction between states, that may be of the same type, with potentially conflicting IDs but are in fact different.
The property name that should be used as a namespace. Namespaces are completely optional, but exist in case you need to make an additional distinction between states, that may be of the same type, with potentially conflicting IDs but are in fact different.

@@ -367,3 +367,3 @@ Defaults to `'namespace'`.

### derived
### derived `AmpersandState.extend({ derived: { derivedProperties }})`

@@ -370,0 +370,0 @@ Derived properties (also known as computed properties) are properties of the state object that depend on other properties (from `props`, `session`, or even `derived`) to determine their value. Best demonstrated with an example:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc