Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

imm

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

imm - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

83

dist/imm.js

@@ -69,3 +69,8 @@ //! Imm

function _checkIsImmutable(object) {
function isObject(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
function _assertIsImmutable(object) {
var is = _isImmutable(object);

@@ -75,3 +80,3 @@ if (!is) throw new Error("Not an immutable object");

function _checkIsPlainArray(array) {
function _assertIsPlainArray(array) {
var one = !_isArray(array);

@@ -82,2 +87,7 @@ var two = _isImmutable(array);

function _assertIsObject(object, msg) {
if (!msg) msg = 'Not an object';
if (!isObject(object)) throw new Error(msg);
}
function _generateUID() {

@@ -91,3 +101,3 @@ var d = new Date().getTime();

return uuid;
};
}

@@ -105,3 +115,3 @@ // Fill in a given object with default properties.

return obj;
};
}

@@ -123,8 +133,9 @@ /**

* @param {Array} records Array of records
* @param {String} [key] Optional name of id key e.g. _id
* @param {Object} [args]
* @param {String} [args.key=id] Optional name of id key e.g. _id
* @return {Imm} Imm collection
* @api public
*/
function imm(records, key) {
return _wrapPlainArray(records, key);
function imm(records, args) {
return _wrapPlainArray(records, args);
}

@@ -134,16 +145,20 @@

// @return {Imm}
function _wrapPlainArray(array, key) {
function _wrapPlainArray(array, args) {
var id, mergable;
if (args) _assertIsObject(args, 'You must provide an object for arguments');
_checkIsPlainArray(array);
var defaults = {
key: DEFAULT_KEY
};
args = _defaults(args, defaults);
key = key || DEFAULT_KEY;
_assertIsPlainArray(array);
// return a immutable object
var col = Immutable(array).asObject(function (record) {
id = record[key];
id = record[args.key];
if (!id) {
id = _generateUID();
mergable = {};
mergable[key] = id;
mergable[args.key] = id;
record = record.merge(mergable);

@@ -154,3 +169,3 @@ }

return _wrapImmutableCollection(col, key);
return _wrapImmutableCollection(col, args);
}

@@ -160,14 +175,12 @@

// @return {Imm}
function _wrapImmutableCollection(immutableCollection, key) {
function _wrapImmutableCollection(immutableCollection, globalArgs) {
_checkIsImmutable(immutableCollection);
_assertIsImmutable(immutableCollection);
key = key || DEFAULT_KEY;
function _wrapPlainArrayWithArgs(array) {
return _wrapPlainArray(array, key);
return _wrapPlainArray(array, globalArgs);
}
function _wrapImmutableCollectionWithArgs(immutableCollection) {
return _wrapImmutableCollection(immutableCollection, key);
return _wrapImmutableCollection(immutableCollection, globalArgs);
}

@@ -203,3 +216,3 @@

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
if (anyExist(ids)) throw new Error('Some records already exist');

@@ -411,3 +424,3 @@ }

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
ids = _idsAsStrings(ids);

@@ -419,3 +432,3 @@ var newCol = immutableCollection.without(ids);

requireKey: true
}
};
args = _defaults(args, defaults);

@@ -425,4 +438,2 @@

// throw if any record exists
var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
if (anyExist(ids)) throw new Error('Some records already exist');

@@ -433,9 +444,9 @@ }

record = records[a];
id = record[key];
id = record[globalArgs.key];
if (!id) {
if (args.requireKey) throw new Error("Record must have ." + key);
if (args.requireKey) throw new Error("Record must have ." + globalArgs.key);
id = _generateUID();
record[key] = id;
record[globalArgs.key] = id;
}
if (!id) throw new Error("Record must have ." + key);
if (!id) throw new Error("Record must have ." + globalArgs.key);
merges[id] = record;

@@ -452,3 +463,3 @@ }

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
ids = _idsAsStrings(ids);

@@ -460,3 +471,3 @@ var newCol = immutableCollection.without(ids);

record = records[a];
id = record[key];
id = record[globalArgs.key];
existing = get(id);

@@ -496,3 +507,3 @@ if (!existing) throw new Error('Record ' + id + ' does not exists');

strict: false
}
};
args = _defaults(args, defaults);

@@ -507,3 +518,3 @@

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
if (anyExist(ids)) throw new Error('Some records already exist');

@@ -514,5 +525,5 @@ }

givenRecord = givenRecords[a];
givenId = givenRecord[key];
givenId = givenRecord[globalArgs.key];
// throw if no givenId
if (!givenId) throw new Error('Record must have .' + key);
if (!givenId) throw new Error('Record must have .' + globalArgs.key);

@@ -542,5 +553,5 @@ existing = immutableCollection[givenId];

givenRecord = givenRecords[a];
givenId = givenRecord[key];
givenId = givenRecord[globalArgs.key];
// throw if no givenId
if (!givenId) throw new Error('Record must have .' + key);
if (!givenId) throw new Error('Record must have .' + globalArgs.key);

@@ -547,0 +558,0 @@ existing = immutableCollection[givenId];

@@ -6,2 +6,2 @@ //! Imm

//! https://github.com/sporto/imm
!function(r,t){if("function"==typeof define&&define.amd)define(["Immutable"],function(e){return r.imm=t(e)});else if("object"==typeof exports){var e=require("seamless-immutable");module.exports=t(e)}else r.imm=t(r.Immutable)}(this,function(r){"use strict";function t(r){return x(r)?r:[r]}function e(r){return r.map(function(r){return""+r})}function n(r,t){if(!t)throw new Error("Must provide a key");return r.map(function(r){return r[t]})}function o(t){return r.isImmutable(t)}function i(r){var t=o(r);if(!t)throw new Error("Not an immutable object")}function u(r){var t=!x(r),e=o(r);if(t||e)throw new Error("You must provide an array")}function a(){var r=(new Date).getTime(),t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=(r+16*Math.random())%16|0;return r=Math.floor(r/16),("x"==t?e:3&e|8).toString(16)});return t}function f(r){r=r||{};for(var t=1,e=arguments.length;e>t;t++){var n=arguments[t];for(var o in n)void 0===r[o]&&(r[o]=n[o])}return r}function c(r,t){return m(r,t)}function m(t,e){var n,o;u(t),e=e||v;var i=r(t).asObject(function(r){return n=r[e],n||(n=a(),o={},o[e]=n,r=r.merge(o)),[n,r]});return s(i,e)}function s(r,o){function u(r){return m(r,o)}function c(r){return s(r,o)}function x(r,e){var i={strict:!1};if(e=f(e,i),e.strict){var u=t(r),a=n(u,o);if(d(a))throw new Error("Some records already exist")}return e.requireKey=!1,I(r,e)}function l(e){for(var n=t(e),o=0;o<n.length;o++){var i=n[o];if(!r[i])return!1}return!0}function d(e){for(var n=t(e),o=0;o<n.length;o++){var i=n[o];if(r[i])return!0}return!1}function h(){return w()}function w(){return Object.keys(r).map(function(t){return r[t].asMutable()})}function y(){return Object.keys(r).length}function g(r){var t=w();return t=t.filter(r),u(t)}function b(r){for(var t=w(),e=0;e<t.length;e++){var n=t[e];if(r(n))return n}return void 0}function p(t){var e=r[t];return e?e.asMutable():void 0}function E(r){var t=w();return t.map(r)}function j(n,o){var i=t(n);if(i=e(i),o&&o.strict&&!l(i))throw new Error("Some records do not exist");var u=r.without(i);return c(u)}function I(i,u){var m,s,v=t(i),x=n(v,o);x=e(x);var l=r.without(x),h={},w={strict:!1,requireKey:!0};if(u=f(u,w),u.strict){var v=t(i),x=n(v,o);if(d(x))throw new Error("Some records already exist")}for(var y=0;y<v.length;y++){if(m=v[y],s=m[o],!s){if(u.requireKey)throw new Error("Record must have ."+o);s=a(),m[o]=s}if(!s)throw new Error("Record must have ."+o);h[s]=m}return l=l.merge(h),c(l)}function S(){return r}function M(e,i){var u={strict:!1};i=f(i,u);var a,m,s,v,x,l=t(e),h=r;if(i.strict){var w=t(e),y=n(w,o);if(d(y))throw new Error("Some records already exist")}for(var g=0;g<l.length;g++){if(m=l[g],a=m[o],!a)throw new Error("Record must have ."+o);v=r[a],x=v?v.merge(m):m,s={},s[a]=x,h=h.merge(s)}return c(h)}return i(r),o=o||v,{isImm:!0,add:x,allExist:l,anyExist:d,array:h,count:y,filter:g,find:b,get:p,replace:I,map:E,remove:j,toImmutable:S,update:M}}if(null==r)throw new Error("Immutable is null");var v="id",x=Array.isArray||function(r){return"[object Array]"===toString.call(r)};return c});
!function(r,t){if("function"==typeof define&&define.amd)define(["Immutable"],function(e){return r.imm=t(e)});else if("object"==typeof exports){var e=require("seamless-immutable");module.exports=t(e)}else r.imm=t(r.Immutable)}(this,function(r){"use strict";function t(r){return y(r)?r:[r]}function e(r){return r.map(function(r){return""+r})}function n(r,t){if(!t)throw new Error("Must provide a key");return r.map(function(r){return r[t]})}function o(t){return r.isImmutable(t)}function u(r){var t=typeof r;return"function"===t||"object"===t&&!!r}function i(r){var t=o(r);if(!t)throw new Error("Not an immutable object")}function a(r){var t=!y(r),e=o(r);if(t||e)throw new Error("You must provide an array")}function f(r,t){if(t||(t="Not an object"),!u(r))throw new Error(t)}function c(){var r=(new Date).getTime(),t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=(r+16*Math.random())%16|0;return r=Math.floor(r/16),("x"==t?e:3&e|8).toString(16)});return t}function m(r){r=r||{};for(var t=1,e=arguments.length;e>t;t++){var n=arguments[t];for(var o in n)void 0===r[o]&&(r[o]=n[o])}return r}function s(r,t){return v(r,t)}function v(t,e){var n,o;e&&f(e,"You must provide an object for arguments");var u={key:l};e=m(e,u),a(t);var i=r(t).asObject(function(r){return n=r[e.key],n||(n=c(),o={},o[e.key]=n,r=r.merge(o)),[n,r]});return x(i,e)}function x(r,o){function u(r){return v(r,o)}function a(r){return x(r,o)}function f(r,e){var u={strict:!1};if(e=m(e,u),e.strict){var i=t(r),a=n(i,o.key);if(l(a))throw new Error("Some records already exist")}return e.requireKey=!1,E(r,e)}function s(e){for(var n=t(e),o=0;o<n.length;o++){var u=n[o];if(!r[u])return!1}return!0}function l(e){for(var n=t(e),o=0;o<n.length;o++){var u=n[o];if(r[u])return!0}return!1}function y(){return d()}function d(){return Object.keys(r).map(function(t){return r[t].asMutable()})}function h(){return Object.keys(r).length}function w(r){var t=d();return t=t.filter(r),u(t)}function g(r){for(var t=d(),e=0;e<t.length;e++){var n=t[e];if(r(n))return n}return void 0}function b(t){var e=r[t];return e?e.asMutable():void 0}function p(r){var t=d();return t.map(r)}function k(n,o){var u=t(n);if(u=e(u),o&&o.strict&&!s(u))throw new Error("Some records do not exist");var i=r.without(u);return a(i)}function E(u,i){var f,s,v=t(u),x=n(v,o.key);x=e(x);var y=r.without(x),d={},h={strict:!1,requireKey:!0};if(i=m(i,h),i.strict&&l(x))throw new Error("Some records already exist");for(var w=0;w<v.length;w++){if(f=v[w],s=f[o.key],!s){if(i.requireKey)throw new Error("Record must have ."+o.key);s=c(),f[o.key]=s}if(!s)throw new Error("Record must have ."+o.key);d[s]=f}return y=y.merge(d),a(y)}function j(){return r}function I(e,u){var i={strict:!1};u=m(u,i);var f,c,s,v,x,y=t(e),d=r;if(u.strict){var h=t(e),w=n(h,o.key);if(l(w))throw new Error("Some records already exist")}for(var g=0;g<y.length;g++){if(c=y[g],f=c[o.key],!f)throw new Error("Record must have ."+o.key);v=r[f],x=v?v.merge(c):c,s={},s[f]=x,d=d.merge(s)}return a(d)}return i(r),{isImm:!0,add:f,allExist:s,anyExist:l,array:y,count:h,filter:w,find:g,get:b,replace:E,map:p,remove:k,toImmutable:j,update:I}}if(null==r)throw new Error("Immutable is null");var l="id",y=Array.isArray||function(r){return"[object Array]"===toString.call(r)};return s});

@@ -5,3 +5,3 @@

## imm(records, [key])
## imm(records, [args], [args.key=id])

@@ -24,3 +24,4 @@ Returns an Imm collection

* **Array** *records* Array of records
* **String** *[key]* Optional name of id key e.g. _id
* **Object** *[args]*
* **String** *[args.key=id]* Optional name of id key e.g. _id

@@ -27,0 +28,0 @@ ### Return:

{
"name": "imm",
"version": "0.4.0",
"version": "0.5.0",
"description": "Immutable collections built on top of immutable.js",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -31,2 +31,4 @@ IMM.js

This library requires [seamless-immutable](https://github.com/rtfeldman/seamless-immutable) to be loaded.
API

@@ -33,0 +35,0 @@ -----------------

@@ -69,3 +69,8 @@ //! Imm

function _checkIsImmutable(object) {
function isObject(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
function _assertIsImmutable(object) {
var is = _isImmutable(object);

@@ -75,3 +80,3 @@ if (!is) throw new Error("Not an immutable object");

function _checkIsPlainArray(array) {
function _assertIsPlainArray(array) {
var one = !_isArray(array);

@@ -82,2 +87,7 @@ var two = _isImmutable(array);

function _assertIsObject(object, msg) {
if (!msg) msg = 'Not an object';
if (!isObject(object)) throw new Error(msg);
}
function _generateUID() {

@@ -91,3 +101,3 @@ var d = new Date().getTime();

return uuid;
};
}

@@ -105,3 +115,3 @@ // Fill in a given object with default properties.

return obj;
};
}

@@ -123,8 +133,9 @@ /**

* @param {Array} records Array of records
* @param {String} [key] Optional name of id key e.g. _id
* @param {Object} [args]
* @param {String} [args.key=id] Optional name of id key e.g. _id
* @return {Imm} Imm collection
* @api public
*/
function imm(records, key) {
return _wrapPlainArray(records, key);
function imm(records, args) {
return _wrapPlainArray(records, args);
}

@@ -134,16 +145,20 @@

// @return {Imm}
function _wrapPlainArray(array, key) {
function _wrapPlainArray(array, args) {
var id, mergable;
if (args) _assertIsObject(args, 'You must provide an object for arguments');
_checkIsPlainArray(array);
var defaults = {
key: DEFAULT_KEY
};
args = _defaults(args, defaults);
key = key || DEFAULT_KEY;
_assertIsPlainArray(array);
// return a immutable object
var col = Immutable(array).asObject(function (record) {
id = record[key];
id = record[args.key];
if (!id) {
id = _generateUID();
mergable = {};
mergable[key] = id;
mergable[args.key] = id;
record = record.merge(mergable);

@@ -154,3 +169,3 @@ }

return _wrapImmutableCollection(col, key);
return _wrapImmutableCollection(col, args);
}

@@ -160,14 +175,12 @@

// @return {Imm}
function _wrapImmutableCollection(immutableCollection, key) {
function _wrapImmutableCollection(immutableCollection, globalArgs) {
_checkIsImmutable(immutableCollection);
_assertIsImmutable(immutableCollection);
key = key || DEFAULT_KEY;
function _wrapPlainArrayWithArgs(array) {
return _wrapPlainArray(array, key);
return _wrapPlainArray(array, globalArgs);
}
function _wrapImmutableCollectionWithArgs(immutableCollection) {
return _wrapImmutableCollection(immutableCollection, key);
return _wrapImmutableCollection(immutableCollection, globalArgs);
}

@@ -203,3 +216,3 @@

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
if (anyExist(ids)) throw new Error('Some records already exist');

@@ -411,3 +424,3 @@ }

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
ids = _idsAsStrings(ids);

@@ -419,3 +432,3 @@ var newCol = immutableCollection.without(ids);

requireKey: true
}
};
args = _defaults(args, defaults);

@@ -425,4 +438,2 @@

// throw if any record exists
var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
if (anyExist(ids)) throw new Error('Some records already exist');

@@ -433,9 +444,9 @@ }

record = records[a];
id = record[key];
id = record[globalArgs.key];
if (!id) {
if (args.requireKey) throw new Error("Record must have ." + key);
if (args.requireKey) throw new Error("Record must have ." + globalArgs.key);
id = _generateUID();
record[key] = id;
record[globalArgs.key] = id;
}
if (!id) throw new Error("Record must have ." + key);
if (!id) throw new Error("Record must have ." + globalArgs.key);
merges[id] = record;

@@ -452,3 +463,3 @@ }

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
ids = _idsAsStrings(ids);

@@ -460,3 +471,3 @@ var newCol = immutableCollection.without(ids);

record = records[a];
id = record[key];
id = record[globalArgs.key];
existing = get(id);

@@ -496,3 +507,3 @@ if (!existing) throw new Error('Record ' + id + ' does not exists');

strict: false
}
};
args = _defaults(args, defaults);

@@ -507,3 +518,3 @@

var records = _wrapAsArray(recordOrRecords);
var ids = _idsFromRecords(records, key);
var ids = _idsFromRecords(records, globalArgs.key);
if (anyExist(ids)) throw new Error('Some records already exist');

@@ -514,5 +525,5 @@ }

givenRecord = givenRecords[a];
givenId = givenRecord[key];
givenId = givenRecord[globalArgs.key];
// throw if no givenId
if (!givenId) throw new Error('Record must have .' + key);
if (!givenId) throw new Error('Record must have .' + globalArgs.key);

@@ -542,5 +553,5 @@ existing = immutableCollection[givenId];

givenRecord = givenRecords[a];
givenId = givenRecord[key];
givenId = givenRecord[globalArgs.key];
// throw if no givenId
if (!givenId) throw new Error('Record must have .' + key);
if (!givenId) throw new Error('Record must have .' + globalArgs.key);

@@ -547,0 +558,0 @@ existing = immutableCollection[givenId];

@@ -85,3 +85,3 @@ var imm = require('../src/imm');

beforeEach(function () {
col = imm(recordWithAltId(), '_id');
col = imm(recordWithAltId(), {key: '_id'});
})

@@ -88,0 +88,0 @@

@@ -5,4 +5,3 @@ var imm = require('../src/imm');

var recordWithAltId = require('./fixtures/records_with_alt_id');
var col1;
var col2;
var col;

@@ -12,16 +11,25 @@

beforeEach(function () {
col1 = imm(records());
col2 = imm(recordWithAltId(), '_id');
})
describe('id', function() {
beforeEach(function () {
col = imm(records());
})
it('returns the count', function (){
var res = col1.count();
expect(res).to.be(2);
});
var res = col.count();
expect(res).to.be(2);
});
})
it('returns the count', function (){
var res = col2.count();
expect(res).to.be(2);
});
describe('_id', function() {
beforeEach(function () {
col = imm(recordWithAltId(), {key: '_id'});
})
it('returns the count', function (){
var res = col.count();
expect(res).to.be(2);
});
})
});

@@ -39,3 +39,3 @@ var imm = require('../src/imm');

beforeEach(function () {
col = imm(recordWithAltId(), '_id');
col = imm(recordWithAltId(), {key: '_id'});
})

@@ -42,0 +42,0 @@

@@ -55,3 +55,3 @@ var imm = require('../src/imm');

}
col = imm(recordWithAltId(), '_id');
col = imm(recordWithAltId(), {key: '_id'});
expect(col.get('xyz')).to.eql(record);

@@ -58,0 +58,0 @@ })

@@ -12,3 +12,2 @@ var imm = require('../src/imm');

col = imm(records());
col2 = imm(recordWithAltId(), '_id');
})

@@ -65,3 +64,3 @@

beforeEach(function () {
col = imm(recordWithAltId(), '_id');
col = imm(recordWithAltId(), {key: '_id'});
})

@@ -71,5 +70,5 @@

it('removes the record', function () {
expect(col2.count()).to.be(2);
col2 = col2.remove('abc');
expect(col2.count()).to.be(1);
expect(col.count()).to.be(2);
col = col.remove('abc');
expect(col.count()).to.be(1);
})

@@ -76,0 +75,0 @@

@@ -77,3 +77,3 @@ var imm = require('../src/imm');

beforeEach(function () {
col = imm(recordWithAltId(), '_id');
col = imm(recordWithAltId(), {key: '_id'});
})

@@ -80,0 +80,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc