Comparing version 0.5.6 to 0.6.0
1407
dist/imm.js
@@ -1,96 +0,360 @@ | ||
//! Imm | ||
//! Immutable collections | ||
//! (c) 2015 Sebastian Porto | ||
//! MIT license. | ||
//! https://github.com/sporto/imm | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(require("seamless-immutable")); | ||
else if(typeof define === 'function' && define.amd) | ||
define(["seamless-immutable"], factory); | ||
else { | ||
var a = typeof exports === 'object' ? factory(require("seamless-immutable")) : factory(root["seamless-immutable"]); | ||
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; | ||
} | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
// CommonJS + AMD + Global boilerplate | ||
// https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ }; | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define(['seamless-immutable'], function (Immutable) { | ||
return (root.imm = factory(Immutable)); | ||
}); | ||
} else if (typeof exports === 'object') { | ||
// Node. Does not work with strict CommonJS, but | ||
// only CommonJS-like enviroments that support module.exports, | ||
// like Node. | ||
var Immutable = require('seamless-immutable'); | ||
module.exports = factory(Immutable); | ||
} else { | ||
// Browser globals | ||
root.imm = factory(root.Immutable); | ||
} | ||
}(this, function (Immutable) { | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
"use strict"; | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
if (Immutable == null) throw new Error('Immutable is null'); | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
var DEFAULT_KEY = 'id'; | ||
// Utility functions | ||
var _isArray = Array.isArray || function(obj) { | ||
return toString.call(obj) === '[object Array]'; | ||
}; | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
function _wrapAsArray(recordOrRecords) { | ||
return _isArray(recordOrRecords) ? recordOrRecords : [recordOrRecords]; | ||
} | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
function _idsAsStrings(array) { | ||
return array.map(function (v) { | ||
return "" + v; | ||
}); | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
//! Imm | ||
//! Immutable collections | ||
//! (c) 2015 Sebastian Porto | ||
//! MIT license. | ||
//! https://github.com/sporto/imm | ||
'use strict'; | ||
var Immutable = __webpack_require__(1); | ||
if (Immutable == null) throw new Error('Immutable is null'); | ||
function imm() { | ||
throw new Error('Using imm directly is deprecated, use imm.list instead'); | ||
} | ||
function _idsFromRecords(array, key) { | ||
if (!key) throw new Error("Must provide a key"); | ||
return array.map(function (record) { | ||
return record[key]; | ||
}); | ||
imm.list = __webpack_require__(2)(Immutable); | ||
imm.obj = __webpack_require__(3)(Immutable); | ||
module.exports = imm; | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
module.exports = __WEBPACK_EXTERNAL_MODULE_1__; | ||
/***/ }, | ||
/* 2 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var wrapPlainArray = __webpack_require__(4); | ||
function makeList(Immutable) { | ||
/** | ||
* Returns an Imm list | ||
* Keys are always sorted in alphabetical order | ||
* | ||
* ### Examples: | ||
* | ||
* var records = [{id: 1, label: 'Sam'}, {...}]; | ||
* collection = Imm.list(records); | ||
* | ||
* Imm assumes that the id key is called `id`. You can provide an optional argument: | ||
* | ||
* collection = Imm.list(records, {key: '_id'}); | ||
* | ||
* @param {Array} records Array of records | ||
* @param {Object} args Optional arguments | ||
* @param {String} args.key=id Optional name of id key e.g. _id | ||
* @return {Imm.list} Imm List | ||
* @api public | ||
*/ | ||
function list(records, args) { | ||
return wrapPlainArray(Immutable, args, records); | ||
} | ||
return list; | ||
} | ||
function _arrayContains(array, value) { | ||
for (var a = 0; a < array.length; a++) { | ||
if (array[a] === value) return true; | ||
module.exports = makeList; | ||
/***/ }, | ||
/* 3 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var isObject = __webpack_require__(5); | ||
var isArray = __webpack_require__(6); | ||
function makeObj(Immutable) { | ||
/** | ||
* Returns an Seamless Immutable object | ||
* See https://github.com/rtfeldman/seamless-immutable#immutable-object | ||
* | ||
* ### Examples: | ||
* | ||
* var data = {id: 1, label: 'Sam'}; | ||
* var record = Imm.obj(data); | ||
* | ||
* To get back a mutable JS object use `asMutable`: | ||
* | ||
* var data = {id: 1, label: 'Sam'}; | ||
* var immutableRecord = Imm.obj(data); | ||
* mutableRecord = immutableRecord.asMutable(); | ||
* | ||
* @param {Object} data A JS object | ||
* @return {SeamlessImmutable.Object} Seamless Immutable object | ||
* @api public | ||
*/ | ||
function obj(data) { | ||
if (!isObject(data) || isArray(data)) throw new Error('You must provide an object'); | ||
return Immutable(data); | ||
} | ||
return false; | ||
return obj; | ||
} | ||
function _isImmutable(object) { | ||
return Immutable.isImmutable(object); | ||
module.exports = makeObj; | ||
/***/ }, | ||
/* 4 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var wrapImmutableCollection = __webpack_require__(7); | ||
var generateUID = __webpack_require__(8); | ||
var mergeDefaults = __webpack_require__(9); | ||
var assertIsObject = __webpack_require__(10); | ||
var assertIsPlainArray = __webpack_require__(11); | ||
var DEFAULT_KEY = 'id'; | ||
/** | ||
* @param {Array} | ||
* @return {Immutable List} | ||
* @api private | ||
*/ | ||
function wrapPlainArray(Immutable, args, array) { | ||
var id; | ||
var mergable; | ||
if (!array) array = []; | ||
if (args) assertIsObject(args, 'You must provide an object for arguments'); | ||
var defaults = { | ||
key: DEFAULT_KEY | ||
}; | ||
args = mergeDefaults(args, defaults); | ||
assertIsPlainArray(Immutable, array); | ||
// return a immutable object | ||
var col = Immutable(array).asObject(function (record) { | ||
id = record[args.key]; | ||
if (!id) { | ||
id = generateUID(); | ||
mergable = {}; | ||
mergable[args.key] = id; | ||
record = record.merge(mergable); | ||
} | ||
return [id, record]; | ||
}); | ||
return wrapImmutableCollection(Immutable, args, col); | ||
} | ||
module.exports = wrapPlainArray; | ||
/***/ }, | ||
/* 5 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
function isObject(obj) { | ||
var type = typeof obj; | ||
return type === 'function' || type === 'object' && !!obj; | ||
return type === 'function' || type === 'object' && !!obj; | ||
} | ||
function _assertIsImmutable(object) { | ||
var is = _isImmutable(object); | ||
if (!is) throw new Error("Not an immutable object"); | ||
} | ||
module.exports = isObject; | ||
function _assertIsPlainArray(array) { | ||
var one = !_isArray(array); | ||
var two = _isImmutable(array); | ||
if (one || two) throw new Error("You must provide an array"); | ||
} | ||
/***/ }, | ||
/* 6 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
function _assertIsObject(object, msg) { | ||
if (!msg) msg = 'Not an object'; | ||
if (!isObject(object)) throw new Error(msg); | ||
/* @flow */ | ||
'use strict'; | ||
module.exports = Array.isArray || function (obj) { | ||
return toString.call(obj) === '[object Array]'; | ||
}; | ||
/***/ }, | ||
/* 7 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var assertIsImmutable = __webpack_require__(12); | ||
var assertIsImmutableInstance = __webpack_require__(13); | ||
/* | ||
* @param {Immutable} | ||
* @return {Imm} | ||
* @api privates | ||
*/ | ||
function wrapImmutableCollection(Immutable, globalArgs, immutableCollection) { | ||
assertIsImmutable(Immutable, Immutable); | ||
assertIsImmutableInstance(Immutable, immutableCollection); | ||
/** | ||
* Convert Imm.List to plain JS array. | ||
* Records in the array are plain mutable JS objects. | ||
* | ||
* ### Example: | ||
* | ||
* var list = collection.asMutable(); | ||
* | ||
* @return {Array} | ||
* @api public | ||
*/ | ||
function asMutable() { | ||
return asPlainArray(Immutable, globalArgs, immutableCollection); | ||
} | ||
function array() { | ||
throw new Error('.array is deprecated, use .asMutable instead'); | ||
} | ||
/** | ||
* Convert Imm list to Seamless Immutable array | ||
* See https://github.com/rtfeldman/seamless-immutable#immutable-array | ||
* | ||
* ### Examples: | ||
* | ||
* var list = collection.unwrap(); | ||
* | ||
* @return {SeamlessImmutable.Array} | ||
* @api public | ||
*/ | ||
function unwrap() { | ||
return immutableCollection; | ||
} | ||
var add = __webpack_require__(14); | ||
var asPlainArray = __webpack_require__(15); | ||
var allExist = __webpack_require__(16); | ||
var anyExist = __webpack_require__(17); | ||
var count = __webpack_require__(18); | ||
var filter = __webpack_require__(19); | ||
var find = __webpack_require__(20); | ||
var get = __webpack_require__(21); | ||
var map = __webpack_require__(22); | ||
var remove = __webpack_require__(23); | ||
var replace = __webpack_require__(24); | ||
var update = __webpack_require__(25); | ||
return { | ||
isImmList: true, | ||
add: add.bind(null, Immutable, globalArgs, immutableCollection), | ||
allExist: allExist.bind(null, Immutable, globalArgs, immutableCollection), | ||
anyExist: anyExist.bind(null, Immutable, globalArgs, immutableCollection), | ||
asMutable: asMutable, | ||
array: array, | ||
count: count.bind(null, Immutable, globalArgs, immutableCollection), | ||
filter: filter.bind(null, Immutable, globalArgs, immutableCollection), | ||
find: find.bind(null, Immutable, globalArgs, immutableCollection), | ||
get: get.bind(null, Immutable, globalArgs, immutableCollection), | ||
replace: replace.bind(null, Immutable, globalArgs, immutableCollection), | ||
map: map.bind(null, Immutable, globalArgs, immutableCollection), | ||
remove: remove.bind(null, Immutable, globalArgs, immutableCollection), | ||
unwrap: unwrap, | ||
update: update.bind(null, Immutable, globalArgs, immutableCollection) | ||
}; | ||
} | ||
function _generateUID() { | ||
module.exports = wrapImmutableCollection; | ||
/***/ }, | ||
/* 8 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
function generateUID() { | ||
var d = new Date().getTime(); | ||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
var r = (d + Math.random()*16)%16 | 0; | ||
d = Math.floor(d/16); | ||
return (c=='x' ? r : (r&0x3|0x8)).toString(16); | ||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = (d + Math.random() * 16) % 16 | 0; | ||
d = Math.floor(d / 16); | ||
return (c == 'x' ? r : r & 3 | 8).toString(16); | ||
}); | ||
@@ -100,5 +364,13 @@ return uuid; | ||
module.exports = generateUID; | ||
/***/ }, | ||
/* 9 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
// Fill in a given object with default properties. | ||
// http://underscorejs.org/#defaults | ||
function _defaults(obj) { | ||
"use strict"; | ||
function defaults(obj) { | ||
obj = obj || {}; | ||
@@ -114,451 +386,676 @@ for (var i = 1, length = arguments.length; i < length; i++) { | ||
module.exports = defaults; | ||
/***/ }, | ||
/* 10 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
var isObject = __webpack_require__(5); | ||
function assertIsObject(object, msg) { | ||
if (!msg) msg = 'Not an object'; | ||
if (!isObject(object)) throw new Error(msg); | ||
} | ||
module.exports = assertIsObject; | ||
/***/ }, | ||
/* 11 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
var isArray = __webpack_require__(6); | ||
var isImmutableInstance = __webpack_require__(26); | ||
function assertIsPlainArray(Immutable, array) { | ||
var one = !isArray(array); | ||
var two = isImmutableInstance(Immutable, array); | ||
if (one || two) throw new Error('You must provide an array'); | ||
} | ||
module.exports = assertIsPlainArray; | ||
/***/ }, | ||
/* 12 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
var isImmutable = __webpack_require__(27); | ||
function assertIsImmutable(Immutable, object) { | ||
var is = isImmutable(Immutable, object); | ||
if (!is) throw new Error('Not Immutable'); | ||
} | ||
module.exports = assertIsImmutable; | ||
/***/ }, | ||
/* 13 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
var isImmutableInstance = __webpack_require__(26); | ||
function assertIsImmutableInstance(Immutable, object) { | ||
var is = isImmutableInstance(Immutable, object); | ||
if (!is) throw new Error('Not an immutable object'); | ||
} | ||
module.exports = assertIsImmutableInstance; | ||
/***/ }, | ||
/* 14 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var replace = __webpack_require__(24); | ||
var anyExist = __webpack_require__(17); | ||
var idsFromRecords = __webpack_require__(28); | ||
var wrapAsArray = __webpack_require__(29); | ||
var mergeDefaults = __webpack_require__(9); | ||
/** | ||
* Returns an Imm collection | ||
* Keys are always sorted in alphabetical order | ||
* | ||
* **Example** | ||
* Adds one or more records. | ||
* If record already exists then it gets replaced. | ||
* If a record doesn't have a key, then the key will be autogenerated. | ||
* | ||
* ```js | ||
* var records = [{id: 1, label: 'Sam'}, {...}]; | ||
* collection = imm(records); | ||
* ``` | ||
* imm assumes that the id key is called `id`. You can provide an optional argument: | ||
* ### Examples: | ||
* | ||
* ```js | ||
* collection = imm(records, {key: '_id'}); | ||
* ``` | ||
* | ||
* @param {Array} records Array of records | ||
* // add one | ||
* collection = collection.add(record) | ||
* | ||
* // add many records | ||
* collection = collection.add(array) | ||
* | ||
* @param {Object|Array} recordOrRecords Record or records to add | ||
* @param {Object} args Optional arguments | ||
* @param {String} args.key=id Optional name of id key e.g. _id | ||
* @return {Imm} Imm collection | ||
* @param {Boolean} args.strict=false Throw if record already exists | ||
* @return {Imm} modified collection | ||
* @api public | ||
*/ | ||
function imm(records, args) { | ||
return _wrapPlainArray(records, args); | ||
function add(Immutable, globalArgs, immutableCollection, recordOrRecords, args) { | ||
var defaults = { | ||
strict: false | ||
}; | ||
args = mergeDefaults(args, defaults); | ||
if (args.strict) { | ||
// throw if any record exists | ||
var records = wrapAsArray(recordOrRecords); | ||
var ids = idsFromRecords(records, globalArgs.key); | ||
var exists = anyExist(Immutable, globalArgs, immutableCollection, ids); | ||
if (exists) throw new Error('Some records already exist'); | ||
} | ||
args.requireKey = false; | ||
return replace(Immutable, globalArgs, immutableCollection, recordOrRecords, args); | ||
} | ||
// @param {Array} | ||
// @return {Imm} | ||
function _wrapPlainArray(array, args) { | ||
var id, mergable; | ||
if (!array) array = []; | ||
module.exports = add; | ||
if (args) _assertIsObject(args, 'You must provide an object for arguments'); | ||
/***/ }, | ||
/* 15 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var defaults = { | ||
key: DEFAULT_KEY | ||
}; | ||
args = _defaults(args, defaults); | ||
/* @flow */ | ||
_assertIsPlainArray(array); | ||
"use strict"; | ||
// return a immutable object | ||
var col = Immutable(array).asObject(function (record) { | ||
id = record[args.key]; | ||
if (!id) { | ||
id = _generateUID(); | ||
mergable = {}; | ||
mergable[args.key] = id; | ||
record = record.merge(mergable); | ||
} | ||
return [id, record]; | ||
function asPlainArray(Immutable, globalArgs, immutableCollection) { | ||
return Object.keys(immutableCollection).map(function (key) { | ||
return immutableCollection[key].asMutable({ deep: true }); | ||
}); | ||
return _wrapImmutableCollection(col, args); | ||
} | ||
// @param {Immutable} | ||
// @return {Imm} | ||
function _wrapImmutableCollection(immutableCollection, globalArgs) { | ||
module.exports = asPlainArray; | ||
_assertIsImmutable(immutableCollection); | ||
/***/ }, | ||
/* 16 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
function _wrapPlainArrayWithArgs(array) { | ||
return _wrapPlainArray(array, globalArgs); | ||
} | ||
/* @flow */ | ||
function _wrapImmutableCollectionWithArgs(immutableCollection) { | ||
return _wrapImmutableCollection(immutableCollection, globalArgs); | ||
} | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
/** | ||
* Adds one or more records. | ||
* If record already exists then it gets replaced. | ||
* If a record doesn't have a key, then the key will be autogenerated. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* // add one | ||
* collection = collection.add(record) | ||
* | ||
* // add many records | ||
* collection = collection.add(array) | ||
* ``` | ||
* | ||
* @param {Object|Array} recordOrRecords Record or records to add | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throw if record already exists | ||
* @return {Imm} modified collection | ||
* @api public | ||
*/ | ||
function add(recordOrRecords, args) { | ||
var defaults = { | ||
strict: false | ||
}; | ||
args = _defaults(args, defaults); | ||
var wrapAsArray = __webpack_require__(29); | ||
if (args.strict) { | ||
// throw if any record exists | ||
var records = _wrapAsArray(recordOrRecords); | ||
var ids = _idsFromRecords(records, globalArgs.key); | ||
if (anyExist(ids)) throw new Error('Some records already exist'); | ||
/** | ||
* Check if the given ID or all given IDs exist. | ||
* | ||
* ### Examples: | ||
* | ||
* var exist = allExist(21); | ||
* var exist = allExist([11, 21]); | ||
* | ||
* @param {Number|String|Array} idOrIds ID or IDs to check | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
function allExist(Immutable, globalArgs, immutableCollection, idOrIds) { | ||
var ids = wrapAsArray(idOrIds); | ||
for (var a = 0; a < ids.length; a++) { | ||
var id = ids[a]; | ||
if (!immutableCollection[id]) { | ||
return false; | ||
} | ||
args.requireKey = false; | ||
return replace(recordOrRecords, args); | ||
} | ||
return true; | ||
} | ||
/** | ||
* Check if the given ID or all given IDs exist. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var exist = allExist(21); | ||
* var exist = allExist([11, 21]); | ||
* ``` | ||
* | ||
* @param {Number|String|Array} idOrIds ID or IDs to check | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
function allExist(idOrIds) { | ||
var ids = _wrapAsArray(idOrIds); | ||
for (var a = 0; a < ids.length; a++) { | ||
var id = ids[a]; | ||
if (!immutableCollection[id]) return false; | ||
module.exports = allExist; | ||
/***/ }, | ||
/* 17 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var wrapAsArray = __webpack_require__(29); | ||
/** | ||
* Check if the given ID or any given IDs exist | ||
* | ||
* ### Examples: | ||
* | ||
* var exist = anyExist(21); | ||
* var exist = anyExist([11, 21]); | ||
* | ||
* @param {Number|String|Array} idOrIds Id or Ids to check | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
function anyExist(Immutable, globalArgs, immutableCollection, idOrIds) { | ||
var ids = wrapAsArray(idOrIds); | ||
for (var a = 0; a < ids.length; a++) { | ||
var id = ids[a]; | ||
if (immutableCollection[id]) { | ||
return true; | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Check if the given ID or any given IDs exist | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var exist = anyExist(21); | ||
* var exist = anyExist([11, 21]); | ||
* ``` | ||
* | ||
* @param {Number|String|Array} idOrIds Id or Ids to check | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
function anyExist(idOrIds) { | ||
var ids = _wrapAsArray(idOrIds); | ||
for (var a = 0; a < ids.length; a++) { | ||
var id = ids[a]; | ||
if (immutableCollection[id]) return true; | ||
module.exports = anyExist; | ||
/***/ }, | ||
/* 18 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/** | ||
* Records count. | ||
* | ||
* ### Example: | ||
* | ||
* count = collection.count(); | ||
* | ||
* @return {Number} count | ||
* @api public | ||
*/ | ||
"use strict"; | ||
function count(Immutable, globalArgs, immutableCollection) { | ||
return Object.keys(immutableCollection).length; | ||
} | ||
module.exports = count; | ||
/***/ }, | ||
/* 19 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var asPlainArray = __webpack_require__(15); | ||
var wrapPlainArray = __webpack_require__(4); | ||
/** | ||
* Filters the collection based on a filtering function. | ||
* Returns a new Imm modified collection | ||
* | ||
* ### Example: | ||
* | ||
* collection = collection.filter(function(record) { | ||
* return record.age > 18; | ||
* }); | ||
* | ||
* @param {Function} filterer Filtering function | ||
* @return {Imm} Modified Imm collection | ||
* @api public | ||
*/ | ||
function filter(Immutable, globalArgs, immutableCollection, filterer) { | ||
var newCol = asPlainArray(Immutable, globalArgs, immutableCollection); | ||
newCol = newCol.filter(filterer); | ||
return wrapPlainArray(Immutable, globalArgs, newCol); | ||
} | ||
module.exports = filter; | ||
/***/ }, | ||
/* 20 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var asPlainArray = __webpack_require__(15); | ||
/** | ||
* Finds one record. | ||
* Returns a plain JS mutable object. | ||
* | ||
* ### Example: | ||
* | ||
* var record = collection.find(function (record) { | ||
* return record.age === 18; | ||
* }); | ||
* | ||
* @param {Function} finder Finder function | ||
* @return {SeamlessImmutable.Object} record Record or undefined | ||
* @api public | ||
*/ | ||
function find(Immutable, globalArgs, immutableCollection, finder) { | ||
var records = asPlainArray(Immutable, globalArgs, immutableCollection); | ||
for (var a = 0; a < records.length; a++) { | ||
var record = records[a]; | ||
if (finder(record)) { | ||
return record; | ||
} | ||
return false; | ||
} | ||
return void 0; | ||
} | ||
/** | ||
* Get all records. | ||
* Records in the array are plain mutable JS objects. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var records = collection.array(); | ||
* ``` | ||
* | ||
* @return {Array} records Plain array with records | ||
* @api public | ||
*/ | ||
function array() { | ||
return asPlainArray(); | ||
} | ||
module.exports = find; | ||
function asPlainArray() { | ||
return Object.keys(immutableCollection).map(function (key) { | ||
return immutableCollection[key].asMutable({deep: true}); | ||
}); | ||
} | ||
/***/ }, | ||
/* 21 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/** | ||
* Records count. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* count = collection.count(); | ||
* ``` | ||
* | ||
* @return {Number} count | ||
* @api public | ||
*/ | ||
function count(){ | ||
return Object.keys(immutableCollection).length; | ||
/* @flow */ | ||
/** | ||
* Get a record by id. | ||
* Returns a Seamless Immutable object | ||
* See https://github.com/rtfeldman/seamless-immutable#immutable-object | ||
* | ||
* ### Examples: | ||
* | ||
* var record = collection.get(11) | ||
* var record = collection.get('11') // same as 11 | ||
* | ||
* To make object mutable use `asMutable()` | ||
* | ||
* var record = collection.get(11) | ||
* record = record.asMutable() | ||
* // or | ||
* record = record.asMutable({deep: true}) | ||
* | ||
* @param {Number|String} id Id to fetch | ||
* @return {Immutale Object} record | ||
* @api public | ||
*/ | ||
"use strict"; | ||
function get(Immutable, globalArgs, immutableCollection, id) { | ||
var record = immutableCollection[id]; | ||
if (!record) { | ||
return void 0; | ||
}return record; | ||
} | ||
module.exports = get; | ||
/***/ }, | ||
/* 22 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var asPlainArray = __webpack_require__(15); | ||
/** | ||
* Map the collection through a given function | ||
* | ||
* ### Examples: | ||
* | ||
* collection = collection.map(function (record) { | ||
* return {foo: record.id}; | ||
* }); | ||
* | ||
* @param {Function} mapper Mapping function | ||
* @return {Array} array | ||
* @api public | ||
*/ | ||
function map(Immutable, globalArgs, immutableCollection, mapper) { | ||
var newCol = asPlainArray(Immutable, globalArgs, immutableCollection); | ||
return newCol.map(mapper); | ||
} | ||
module.exports = map; | ||
/***/ }, | ||
/* 23 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var idsAsStrings = __webpack_require__(30); | ||
var allExist = __webpack_require__(16); | ||
var wrapAsArray = __webpack_require__(29); | ||
var wrapImmutableCollection = __webpack_require__(7); | ||
/** | ||
* Removes one or many records based on the id. | ||
* If record is not found then it just gets skipped. | ||
* | ||
* ### Examples: | ||
* | ||
* collection = collection.remove(id); | ||
* collection = collection.remove(arrayOfIds); | ||
* | ||
* @param {Number|String|Array} idOrIds Id or ids to remove | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throw if record(s) doesn't exists | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function remove(Immutable, globalArgs, immutableCollection, idOrIds, args) { | ||
var ids = wrapAsArray(idOrIds); | ||
// ids need to be strings for without | ||
ids = idsAsStrings(ids); | ||
if (args && args.strict) { | ||
var exists = allExist(Immutable, globalArgs, immutableCollection, ids); | ||
if (!exists) throw new Error('Some records do not exist'); | ||
} | ||
/** | ||
* Filters the collection based on a filtering function. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.filter(function (record) { | ||
* return record.age > 18; | ||
* }); | ||
* ``` | ||
* | ||
* @param {Function} filterer Filtering function | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function filter(filterer) { | ||
var newCol = asPlainArray(); | ||
newCol = newCol.filter(filterer); | ||
return _wrapPlainArrayWithArgs(newCol); | ||
var newCol = immutableCollection.without(ids); | ||
return wrapImmutableCollection(Immutable, globalArgs, newCol); | ||
} | ||
module.exports = remove; | ||
/***/ }, | ||
/* 24 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var idsAsStrings = __webpack_require__(30); | ||
var idsFromRecords = __webpack_require__(28); | ||
var mergeDefaults = __webpack_require__(9); | ||
var wrapAsArray = __webpack_require__(29); | ||
var generateUID = __webpack_require__(8); | ||
var anyExist = __webpack_require__(17); | ||
var wrapImmutableCollection = __webpack_require__(7); | ||
/** | ||
* Replaces one item or many. | ||
* This discards any previous data from the replaced items. | ||
* If records doesn't exist then it just gets added. | ||
* This throws if a record doesn't have an key. | ||
* | ||
* ### Examples: | ||
* | ||
* collection = collection.replace(record) | ||
* collection = collection.replace(array) | ||
* | ||
* @param {Object} recordOrRecords Record or records to replace | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throws if record exist | ||
* @param {Boolean} args.requireKey=true Throws if record doesn't have a key | ||
* @return {Imm} Modified Imm collection | ||
* @api public | ||
*/ | ||
function replace(Immutable, globalArgs, immutableCollection, recordOrRecords, args) { | ||
var record; | ||
var id; | ||
var records = wrapAsArray(recordOrRecords); | ||
var ids = idsFromRecords(records, globalArgs.key); | ||
ids = idsAsStrings(ids); | ||
var newCol = immutableCollection.without(ids); | ||
var merges = {}; | ||
var defaults = { | ||
strict: false, | ||
requireKey: true | ||
}; | ||
args = mergeDefaults(args, defaults); | ||
if (args.strict) { | ||
// throw if any record exists | ||
var exists = anyExist(Immutable, globalArgs, immutableCollection, ids); | ||
if (exists) throw new Error('Some records already exist'); | ||
} | ||
/** | ||
* Finds one record. | ||
* Returns a plain JS mutable object. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var record = collection.find(function (record) { | ||
* return record.age === 18; | ||
* }); | ||
* ``` | ||
* | ||
* @param {Function} finder Finder function | ||
* @return {Object} record Record or undefined | ||
* @api public | ||
*/ | ||
function find(finder) { | ||
var records = asPlainArray(); | ||
for (var a = 0; a < records.length; a++) { | ||
var record = records[a]; | ||
if (finder(record)) { | ||
return record; | ||
} | ||
for (var a = 0; a < records.length; a++) { | ||
record = records[a]; | ||
id = record[globalArgs.key]; | ||
if (!id) { | ||
if (args.requireKey) throw new Error('Record must have .' + globalArgs.key); | ||
id = generateUID(); | ||
record[globalArgs.key] = id; | ||
} | ||
return void(0); | ||
if (!id) throw new Error('Record must have .' + globalArgs.key); | ||
merges[id] = record; | ||
} | ||
/** | ||
* Get a record. | ||
* Returned record is a plain JS mutable object. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var record = collection.get(11) | ||
* var record = collection.get('11') // same as 11 | ||
* ``` | ||
* | ||
* @param {Number|String} id Id to fetch | ||
* @return {Object} record | ||
* @api public | ||
*/ | ||
function get(id) { | ||
var record = immutableCollection[id]; | ||
if (!record) return void(0); | ||
return record.asMutable({deep: true}); | ||
} | ||
newCol = newCol.merge(merges); | ||
/** | ||
* Map the collection through a given function | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.map(function (record) { | ||
* return {foo: record.id}; | ||
* }); | ||
* ``` | ||
* | ||
* @param {Function} mapper Mapping function | ||
* @return {Array} array | ||
* @api public | ||
*/ | ||
function map(mapper) { | ||
var newCol = asPlainArray(); | ||
return newCol.map(mapper); | ||
} | ||
return wrapImmutableCollection(Immutable, globalArgs, newCol); | ||
} | ||
/** | ||
* Removes one or many records based on the id. | ||
* If record is not found then it just gets skipped. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.remove(id); | ||
* collection = collection.remove(arrayOfIds); | ||
* ``` | ||
* | ||
* @param {Number|String|Array} idOrIds Id or ids to remove | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throw if record(s) doesn't exists | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function remove(idOrIds, args) { | ||
var ids = _wrapAsArray(idOrIds); | ||
module.exports = replace; | ||
// ids need to be strings for without | ||
ids = _idsAsStrings(ids); | ||
/***/ }, | ||
/* 25 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
if (args && args.strict) { | ||
if (!allExist(ids)) throw new Error('Some records do not exist'); | ||
} | ||
/* @flow */ | ||
var newCol = immutableCollection.without(ids); | ||
return _wrapImmutableCollectionWithArgs(newCol); | ||
} | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
/** | ||
* Replaces one item or many. | ||
* This discards any previous data from the replaced items. | ||
* If records doesn't exist then it just gets added. | ||
* This throws if a record doesn't have an key. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.replace(record) | ||
* collection = collection.replace(array) | ||
* ``` | ||
* | ||
* @param {Object} recordOrRecords Record or records to replace | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throws if record exist | ||
* @param {Boolean} args.requireKey=true Throws if record doesn't have a key | ||
* @return {Imm} Modified Imm collection | ||
* @api public | ||
*/ | ||
function replace(recordOrRecords, args) { | ||
var record, id; | ||
var records = _wrapAsArray(recordOrRecords); | ||
var ids = _idsFromRecords(records, globalArgs.key); | ||
ids = _idsAsStrings(ids); | ||
var newCol = immutableCollection.without(ids); | ||
var merges = {}; | ||
var defaults = { | ||
strict: false, | ||
requireKey: true | ||
}; | ||
args = _defaults(args, defaults); | ||
var idsFromRecords = __webpack_require__(28); | ||
var wrapAsArray = __webpack_require__(29); | ||
var mergeDefaults = __webpack_require__(9); | ||
var anyExist = __webpack_require__(17); | ||
var wrapImmutableCollection = __webpack_require__(7); | ||
if (args.strict) { | ||
// throw if any record exists | ||
if (anyExist(ids)) throw new Error('Some records already exist'); | ||
} | ||
/** | ||
* Updates one record or many. | ||
* This merges the given data with the existing one. | ||
* If a record is not found then it gets added. | ||
* This throws if a record doesn't have an key | ||
* | ||
* ### Examples: | ||
* | ||
* collection = collection.update(record) | ||
* collection = collection.update(array) | ||
* | ||
* @param {Object|Array} recordOrRecords Record or records to update | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throws if record exist | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function update(Immutable, globalArgs, immutableCollection, recordOrRecords, args) { | ||
for (var a = 0; a < records.length; a++) { | ||
record = records[a]; | ||
id = record[globalArgs.key]; | ||
if (!id) { | ||
if (args.requireKey) throw new Error("Record must have ." + globalArgs.key); | ||
id = _generateUID(); | ||
record[globalArgs.key] = id; | ||
} | ||
if (!id) throw new Error("Record must have ." + globalArgs.key); | ||
merges[id] = record; | ||
} | ||
var defaults = { | ||
strict: false | ||
}; | ||
args = mergeDefaults(args, defaults); | ||
newCol = newCol.merge(merges); | ||
var givenId; | ||
var givenRecord; | ||
var toMerge; | ||
var existing; | ||
var mergedRecord; | ||
var givenRecords = wrapAsArray(recordOrRecords); | ||
var newCol = immutableCollection; | ||
return _wrapImmutableCollectionWithArgs(newCol); | ||
if (args.strict) { | ||
// throw if any record exists | ||
var records = wrapAsArray(recordOrRecords); | ||
var ids = idsFromRecords(records, globalArgs.key); | ||
var exists = anyExist(Immutable, globalArgs, immutableCollection, ids); | ||
if (exists) throw new Error('Some records already exist'); | ||
} | ||
function toImmutable() { | ||
return immutableCollection; | ||
for (var a = 0; a < givenRecords.length; a++) { | ||
givenRecord = givenRecords[a]; | ||
givenId = givenRecord[globalArgs.key]; | ||
// throw if no givenId | ||
if (!givenId) throw new Error('Record must have .' + globalArgs.key); | ||
existing = immutableCollection[givenId]; | ||
if (existing) { | ||
mergedRecord = existing.merge(givenRecord); | ||
} else { | ||
mergedRecord = givenRecord; | ||
} | ||
toMerge = {}; | ||
toMerge[givenId] = mergedRecord; | ||
newCol = newCol.merge(toMerge); | ||
} | ||
/** | ||
* Updates one record or many. | ||
* This merges the given data with the existing one. | ||
* If a record is not found then it gets added. | ||
* This throws if a record doesn't have an key | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.update(record) | ||
* collection = collection.update(array) | ||
* ``` | ||
* | ||
* @param {Object|Array} recordOrRecords Record or records to update | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throws if record exist | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function update(recordOrRecords, args) { | ||
var defaults = { | ||
strict: false | ||
}; | ||
args = _defaults(args, defaults); | ||
return wrapImmutableCollection(Immutable, globalArgs, newCol); | ||
} | ||
var givenId, givenRecord, toMerge, existing, mergedRecord; | ||
var givenRecords = _wrapAsArray(recordOrRecords); | ||
var newCol = immutableCollection; | ||
module.exports = update; | ||
if (args.strict) { | ||
// throw if any record exists | ||
var records = _wrapAsArray(recordOrRecords); | ||
var ids = _idsFromRecords(records, globalArgs.key); | ||
if (anyExist(ids)) throw new Error('Some records already exist'); | ||
} | ||
/***/ }, | ||
/* 26 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
for (var a = 0; a < givenRecords.length; a++) { | ||
givenRecord = givenRecords[a]; | ||
givenId = givenRecord[globalArgs.key]; | ||
// throw if no givenId | ||
if (!givenId) throw new Error('Record must have .' + globalArgs.key); | ||
/* @flow */ | ||
existing = immutableCollection[givenId]; | ||
if (existing) { | ||
mergedRecord = existing.merge(givenRecord); | ||
} else { | ||
mergedRecord = givenRecord; | ||
} | ||
"use strict"; | ||
toMerge = {}; | ||
toMerge[givenId] = mergedRecord; | ||
function isImmutableInstance(Immutable, object) { | ||
return Immutable.isImmutable(object); | ||
} | ||
newCol = newCol.merge(toMerge); | ||
} | ||
module.exports = isImmutableInstance; | ||
return _wrapImmutableCollectionWithArgs(newCol); | ||
} | ||
/***/ }, | ||
/* 27 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
return { | ||
isImm: true, | ||
add: add, | ||
allExist: allExist, | ||
anyExist: anyExist, | ||
array: array, | ||
count: count, | ||
filter: filter, | ||
find: find, | ||
get: get, | ||
replace: replace, | ||
map: map, | ||
remove: remove, | ||
toImmutable: toImmutable, | ||
update: update, | ||
}; | ||
/* @flow */ | ||
"use strict"; | ||
function isImmutable(Immutable, object) { | ||
return object.isImmutable != null; | ||
} | ||
return imm; | ||
module.exports = isImmutable; | ||
})); | ||
/***/ }, | ||
/* 28 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/* @flow */ | ||
'use strict'; | ||
function idsFromRecords(array, key) { | ||
if (!key) throw new Error('Must provide a key'); | ||
return array.map(function (record) { | ||
return record[key]; | ||
}); | ||
} | ||
module.exports = idsFromRecords; | ||
/***/ }, | ||
/* 29 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
var isArray = __webpack_require__(6); | ||
function wrapAsArray(recordOrRecords) { | ||
return isArray(recordOrRecords) ? recordOrRecords : [recordOrRecords]; | ||
}; | ||
module.exports = wrapAsArray; | ||
/***/ }, | ||
/* 30 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
function idsAsStrings(array) { | ||
return array.map(function (v) { | ||
return '' + v; | ||
}); | ||
} | ||
module.exports = idsAsStrings; | ||
/***/ } | ||
/******/ ]) | ||
}); | ||
; |
@@ -1,2 +0,2 @@ | ||
//! Imm | ||
!function(r,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("seamless-immutable"));else if("function"==typeof define&&define.amd)define(["seamless-immutable"],t);else{var e=t("object"==typeof exports?require("seamless-immutable"):r["seamless-immutable"]);for(var n in e)("object"==typeof exports?exports:r)[n]=e[n]}}(this,function(r){return function(r){function t(n){if(e[n])return e[n].exports;var o=e[n]={exports:{},id:n,loaded:!1};return r[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var e={};return t.m=r,t.c=e,t.p="",t(0)}([function(r,t,e){//! Imm | ||
//! Immutable collections | ||
@@ -6,2 +6,41 @@ //! (c) 2015 Sebastian Porto | ||
//! https://github.com/sporto/imm | ||
!function(r,e){if("function"==typeof define&&define.amd)define(["seamless-immutable"],function(t){return r.imm=e(t)});else if("object"==typeof exports){var t=require("seamless-immutable");module.exports=e(t)}else r.imm=e(r.Immutable)}(this,function(r){"use strict";function e(r){return y(r)?r:[r]}function t(r){return r.map(function(r){return""+r})}function n(r,e){if(!e)throw new Error("Must provide a key");return r.map(function(r){return r[e]})}function o(e){return r.isImmutable(e)}function u(r){var e=typeof r;return"function"===e||"object"===e&&!!r}function i(r){var e=o(r);if(!e)throw new Error("Not an immutable object")}function a(r){var e=!y(r),t=o(r);if(e||t)throw new Error("You must provide an array")}function f(r,e){if(e||(e="Not an object"),!u(r))throw new Error(e)}function c(){var r=(new Date).getTime(),e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=(r+16*Math.random())%16|0;return r=Math.floor(r/16),("x"==e?t:3&t|8).toString(16)});return e}function m(r){r=r||{};for(var e=1,t=arguments.length;t>e;e++){var n=arguments[e];for(var o in n)void 0===r[o]&&(r[o]=n[o])}return r}function s(r,e){return v(r,e)}function v(e,t){var n,o;e||(e=[]),t&&f(t,"You must provide an object for arguments");var u={key:l};t=m(t,u),a(e);var i=r(e).asObject(function(r){return n=r[t.key],n||(n=c(),o={},o[t.key]=n,r=r.merge(o)),[n,r]});return x(i,t)}function x(r,o){function u(r){return v(r,o)}function a(r){return x(r,o)}function f(r,t){var u={strict:!1};if(t=m(t,u),t.strict){var i=e(r),a=n(i,o.key);if(l(a))throw new Error("Some records already exist")}return t.requireKey=!1,E(r,t)}function s(t){for(var n=e(t),o=0;o<n.length;o++){var u=n[o];if(!r[u])return!1}return!0}function l(t){for(var n=e(t),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(e){return r[e].asMutable({deep:!0})})}function h(){return Object.keys(r).length}function w(r){var e=d();return e=e.filter(r),u(e)}function g(r){for(var e=d(),t=0;t<e.length;t++){var n=e[t];if(r(n))return n}return void 0}function b(e){var t=r[e];return t?t.asMutable({deep:!0}):void 0}function p(r){var e=d();return e.map(r)}function k(n,o){var u=e(n);if(u=t(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=e(u),x=n(v,o.key);x=t(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 S(t,u){var i={strict:!1};u=m(u,i);var f,c,s,v,x,y=e(t),d=r;if(u.strict){var h=e(t),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:S}}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}); | ||
"use strict";function n(){throw new Error("Using imm directly is deprecated, use imm.list instead")}var o=e(1);if(null==o)throw new Error("Immutable is null");n.list=e(2)(o),n.obj=e(3)(o),r.exports=n},function(t){t.exports=r},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r){function t(t,e){return o(r,e,t)}return t}var o=e(4);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r){function t(t){if(!o(t)||i(t))throw new Error("You must provide an object");return r(t)}return t}var o=e(5),i=e(6);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e){var n,f;e||(e=[]),t&&s(t,"You must provide an object for arguments");var x={key:a};t=u(t,x),c(r,e);var l=r(e).asObject(function(r){return n=r[t.key],n||(n=i(),f={},f[t.key]=n,r=r.merge(f)),[n,r]});return o(r,t,l)}var o=e(7),i=e(8),u=e(9),s=e(10),c=e(11),a="id";r.exports=n},function(r){"use strict";function t(r){var t=typeof r;return"function"===t||"object"===t&&!!r}r.exports=t},function(r){"use strict";r.exports=Array.isArray||function(r){return"[object Array]"===toString.call(r)}},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,n){function u(){return f(r,t,n)}function s(){throw new Error(".array is deprecated, use .asMutable instead")}function c(){return n}o(r,r),i(r,n);var a=e(14),f=e(15),x=e(16),l=e(17),v=e(18),p=e(19),m=e(20),d=e(21),b=e(22),y=e(23),w=e(24),h=e(25);return{isImmList:!0,add:a.bind(null,r,t,n),allExist:x.bind(null,r,t,n),anyExist:l.bind(null,r,t,n),asMutable:u,array:s,count:v.bind(null,r,t,n),filter:p.bind(null,r,t,n),find:m.bind(null,r,t,n),get:d.bind(null,r,t,n),replace:w.bind(null,r,t,n),map:b.bind(null,r,t,n),remove:y.bind(null,r,t,n),unwrap:c,update:h.bind(null,r,t,n)}}var o=e(12),i=e(13);r.exports=n},function(r){"use strict";function t(){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}r.exports=t},function(r){"use strict";function t(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}r.exports=t},function(r,t,e){"use strict";function n(r,t){if(t||(t="Not an object"),!o(r))throw new Error(t)}var o=e(5);r.exports=n},function(r,t,e){"use strict";function n(r,t){var e=!o(t),n=i(r,t);if(e||n)throw new Error("You must provide an array")}var o=e(6),i=e(26);r.exports=n},function(r,t,e){"use strict";function n(r,t){var e=o(r,t);if(!e)throw new Error("Not Immutable")}var o=e(27);r.exports=n},function(r,t,e){"use strict";function n(r,t){var e=o(r,t);if(!e)throw new Error("Not an immutable object")}var o=e(26);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n,a){var f={strict:!1};if(a=c(a,f),a.strict){var x=s(n),l=u(x,t.key),v=i(r,t,e,l);if(v)throw new Error("Some records already exist")}return a.requireKey=!1,o(r,t,e,n,a)}var o=e(24),i=e(17),u=e(28),s=e(29),c=e(9);r.exports=n},function(r){"use strict";function t(r,t,e){return Object.keys(e).map(function(r){return e[r].asMutable({deep:!0})})}r.exports=t},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n){for(var i=o(n),u=0;u<i.length;u++){var s=i[u];if(!e[s])return!1}return!0}var o=e(29);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n){for(var i=o(n),u=0;u<i.length;u++){var s=i[u];if(e[s])return!0}return!1}var o=e(29);r.exports=n},function(r){"use strict";function t(r,t,e){return Object.keys(e).length}r.exports=t},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n){var u=o(r,t,e);return u=u.filter(n),i(r,t,u)}var o=e(15),i=e(4);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n){for(var i=o(r,t,e),u=0;u<i.length;u++){var s=i[u];if(n(s))return s}return void 0}var o=e(15);r.exports=n},function(r){"use strict";function t(r,t,e,n){var o=e[n];return o?o:void 0}r.exports=t},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n){var i=o(r,t,e);return i.map(n)}var o=e(15);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n,c){var a=u(n);if(a=o(a),c&&c.strict){var f=i(r,t,e,a);if(!f)throw new Error("Some records do not exist")}var x=e.without(a);return s(r,t,x)}var o=e(30),i=e(16),u=e(29),s=e(7);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n,x){var l,v,p=s(n),m=i(p,t.key);m=o(m);var d=e.without(m),b={},y={strict:!1,requireKey:!0};if(x=u(x,y),x.strict){var w=a(r,t,e,m);if(w)throw new Error("Some records already exist")}for(var h=0;h<p.length;h++){if(l=p[h],v=l[t.key],!v){if(x.requireKey)throw new Error("Record must have ."+t.key);v=c(),l[t.key]=v}if(!v)throw new Error("Record must have ."+t.key);b[v]=l}return d=d.merge(b),f(r,t,d)}var o=e(30),i=e(28),u=e(9),s=e(29),c=e(8),a=e(17),f=e(7);r.exports=n},function(r,t,e){/*! | ||
* Module dependencies. | ||
*/ | ||
"use strict";function n(r,t,e,n,a){var f={strict:!1};a=u(a,f);var x,l,v,p,m,d=i(n),b=e;if(a.strict){var y=i(n),w=o(y,t.key),h=s(r,t,e,w);if(h)throw new Error("Some records already exist")}for(var g=0;g<d.length;g++){if(l=d[g],x=l[t.key],!x)throw new Error("Record must have ."+t.key);p=e[x],m=p?p.merge(l):l,v={},v[x]=m,b=b.merge(v)}return c(r,t,b)}var o=e(28),i=e(29),u=e(9),s=e(17),c=e(7);r.exports=n},function(r){"use strict";function t(r,t){return r.isImmutable(t)}r.exports=t},function(r){"use strict";function t(r,t){return null!=t.isImmutable}r.exports=t},function(r){"use strict";function t(r,t){if(!t)throw new Error("Must provide a key");return r.map(function(r){return r[t]})}r.exports=t},function(r,t,e){"use strict";function n(r){return o(r)?r:[r]}var o=e(6);r.exports=n},function(r){"use strict";function t(r){return r.map(function(r){return""+r})}r.exports=t}])}); |
@@ -6,4 +6,3 @@ 'use strict'; | ||
var uglify = require('gulp-uglify'); | ||
var markdox = require("gulp-markdox"); | ||
var jshint = require('gulp-jshint'); | ||
var jscs = require('gulp-jscs'); | ||
var stylish = require('jshint-stylish'); | ||
@@ -13,30 +12,61 @@ var mocha = require('gulp-mocha'); | ||
var run = require('gulp-run'); | ||
var concat = require('gulp-concat'); | ||
var webpack = require('gulp-webpack-build'); | ||
var path = require('path'); | ||
var markdox = require('gulp-markdox'); | ||
// register verb helpers | ||
// verb.helper('apidocs', verbApiDocs); | ||
require('babel/register'); | ||
var DEST = 'dist/'; | ||
gulp.task('lint', function() { | ||
return gulp.src('./src/*.js') | ||
.pipe(jshint()) | ||
.pipe(jshint.reporter(stylish)); | ||
return gulp.src('./src/**/*.js') | ||
.pipe(jscs()); | ||
}); | ||
gulp.task('test', function () { | ||
return gulp.src("./test/*.js", {read: false}) | ||
.pipe(mocha({reporter: 'nyan'})); | ||
gulp.task('test', function() { | ||
var mochaOptions = { | ||
reporter: 'nyan' | ||
} | ||
return gulp.src('./test/**/*.coffee', {read: false}) | ||
.pipe(mocha(mochaOptions)); | ||
}); | ||
gulp.task('min', function() { | ||
return gulp.src('src/imm.js') | ||
// This will output the non-minified version | ||
.pipe(gulp.dest(DEST)) | ||
// This will minify and rename to foo.min.js | ||
.pipe(uglify({preserveComments: 'some'})) | ||
.pipe(rename({ extname: '.min.js' })) | ||
.pipe(gulp.dest(DEST)); | ||
gulp.task('test-watch', function() { | ||
gulp.watch(['src/**', 'test/**'], ['test']); | ||
}); | ||
gulp.task('doc', function () { | ||
run('verb').exec(); | ||
}) | ||
gulp.task('bundle', function() { | ||
return gulp.src('webpack.config.js') | ||
.pipe(webpack.compile()) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
gulp.task('default', ['test', 'lint', 'min', 'doc']); | ||
gulp.task('min', ['bundle'], function() { | ||
return gulp.src('dist/imm.js') | ||
.pipe(uglify({preserveComments: 'some'})) | ||
.pipe(rename({extname: '.min.js'})) | ||
.pipe(gulp.dest(DEST)); | ||
}); | ||
// this doesnt work as expected | ||
gulp.task('doc-imm', function() { | ||
gulp.src('./src/*.js') | ||
.pipe(markdox()) | ||
.pipe(concat('imm.md')) | ||
.pipe(gulp.dest('./docs')); | ||
}); | ||
gulp.task('doc-list', function() { | ||
gulp.src('./src/list/*.js') | ||
.pipe(markdox()) | ||
.pipe(concat('list.md')) | ||
.pipe(gulp.dest('./docs')); | ||
}); | ||
gulp.task('doc', ['doc-imm', 'doc-list']); | ||
gulp.task('default', ['test', 'lint', 'min', 'doc']); |
{ | ||
"name": "imm", | ||
"version": "0.5.6", | ||
"version": "0.6.0", | ||
"description": "Immutable collections built on top of immutable.js", | ||
@@ -10,3 +10,3 @@ "main": "dist/imm.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha test/**/*_spec.*" | ||
}, | ||
@@ -24,6 +24,13 @@ "repository": { | ||
"devDependencies": { | ||
"expect.js": "^0.3.1", | ||
"babel": "^5.2.5", | ||
"babel-core": "^5.2.6", | ||
"babel-loader": "^5.0.0", | ||
"chai": "^2.3.0", | ||
"coffee-script": "^1.9.2", | ||
"esprima-fb": "^15001.1.0-dev-harmony-fb", | ||
"gulp": "^3.8.8", | ||
"gulp-concat": "^2.5.2", | ||
"gulp-header": "^1.1.1", | ||
"gulp-jshint": "^1.8.5", | ||
"gulp-jscs": "^1.6.0", | ||
"gulp-markdox": "^0.1.1", | ||
"gulp-mocha": "^1.1.1", | ||
@@ -33,5 +40,8 @@ "gulp-rename": "^1.2.0", | ||
"gulp-uglify": "^1.0.1", | ||
"gulp-webpack-build": "^0.7.1", | ||
"helper-apidocs": "^0.4.1", | ||
"jshint-stylish": "^1.0.0", | ||
"markdox": "^0.1.5", | ||
"mocha": "^1.21.4" | ||
"mocha": "^1.21.4", | ||
"webpack": "^1.8.11" | ||
}, | ||
@@ -38,0 +48,0 @@ "dependencies": { |
220
readme.md
@@ -6,3 +6,3 @@ IMM.js | ||
__Immutable__ data collections build on top of [seamless-immutable](https://github.com/rtfeldman/seamless-immutable) | ||
__Immutable__ data collections built on top of [seamless-immutable](https://github.com/rtfeldman/seamless-immutable) | ||
@@ -19,4 +19,3 @@ Seemless-immutable.js is great, but it doesn't have an API that feels right for CRUD applications. | ||
Install | ||
------- | ||
## Install | ||
@@ -36,212 +35,21 @@ ### Using NPM | ||
## API | ||
### [imm](src/imm.js#L136) | ||
Returns an Imm collection Keys are always sorted in alphabetical order | ||
./docs/imm.md | ||
./docs/list.md | ||
* **{Array}**: records Array of records | ||
* **{Object}**: args Optional arguments | ||
* **{String}**: args.key=id Optional name of id key e.g. _id | ||
* `returns` **{Imm}**: Imm collection | ||
### Things to note | ||
**Example** | ||
- Methods that return collections (e.g. filter) return an instance of Imm | ||
- Methods that return one record (e.g. get, find), return a plain JS object | ||
```js | ||
var records = [{id: 1, label: 'Sam'}, {...}]; | ||
collection = imm(records); | ||
``` | ||
## Development | ||
imm assumes that the id key is called `id`. You can provide an optional argument: | ||
To generate documentation `verb` needs to be installed: | ||
```js | ||
collection = imm(records, {key: '_id'}); | ||
``` | ||
### [add](src/imm.js#L205) | ||
Adds one or more records. If record already exists then it gets replaced. If a record doesn't have a key, then the key will be autogenerated. | ||
* **{Object|Array}**: recordOrRecords Record or records to add | ||
* **{Object}**: args Optional arguments | ||
* **{Boolean}**: args.strict=false Throw if record already exists | ||
* `returns` **{Imm}**: modified collection | ||
**Example** | ||
```js | ||
// add one | ||
collection = collection.add(record) | ||
// add many records | ||
collection = collection.add(array) | ||
npm i -g verb-cli | ||
``` | ||
### [allExist](src/imm.js#L235) | ||
### Testing | ||
Check if the given ID or all given IDs exist. | ||
* **{Number|String|Array}**: idOrIds ID or IDs to check | ||
* `returns`: {Boolean} | ||
**Example** | ||
```js | ||
var exist = allExist(21); | ||
var exist = allExist([11, 21]); | ||
``` | ||
### [anyExist](src/imm.js#L258) | ||
Check if the given ID or any given IDs exist | ||
* **{Number|String|Array}**: idOrIds Id or Ids to check | ||
* `returns`: {Boolean} | ||
**Example** | ||
```js | ||
var exist = anyExist(21); | ||
var exist = anyExist([11, 21]); | ||
``` | ||
### [array](src/imm.js#L280) | ||
Get all records. Records in the array are plain mutable JS objects. | ||
* `returns` **{Array}**: records Plain array with records | ||
**Example** | ||
```js | ||
var records = collection.array(); | ||
``` | ||
### [count](src/imm.js#L302) | ||
Records count. | ||
* `returns` **{Number}**: count | ||
**Example** | ||
```js | ||
count = collection.count(); | ||
``` | ||
### [filter](src/imm.js#L321) | ||
Filters the collection based on a filtering function. | ||
* **{Function}**: filterer Filtering function | ||
* `returns` **{Imm}**: Modified collection | ||
**Example** | ||
```js | ||
collection = collection.filter(function (record) { | ||
return record.age > 18; | ||
}); | ||
``` | ||
### [find](src/imm.js#L343) | ||
Finds one record. Returns a plain JS mutable object. | ||
* **{Function}**: finder Finder function | ||
* `returns` **{Object}**: record Record or undefined | ||
**Example** | ||
```js | ||
var record = collection.find(function (record) { | ||
return record.age === 18; | ||
}); | ||
``` | ||
### [get](src/imm.js#L369) | ||
Get a record. Returned record is a plain JS mutable object. | ||
* **{Number|String}**: id Id to fetch | ||
* `returns` **{Object}**: record | ||
**Example** | ||
```js | ||
var record = collection.get(11) | ||
var record = collection.get('11') // same as 11 | ||
``` | ||
### [map](src/imm.js#L390) | ||
Map the collection through a given function | ||
* **{Function}**: mapper Mapping function | ||
* `returns` **{Array}**: array | ||
**Example** | ||
```js | ||
collection = collection.map(function (record) { | ||
return {foo: record.id}; | ||
}); | ||
``` | ||
### [remove](src/imm.js#L412) | ||
Removes one or many records based on the id. If record is not found then it just gets skipped. | ||
* **{Number|String|Array}**: idOrIds Id or ids to remove | ||
* **{Object}**: args Optional arguments | ||
* **{Boolean}**: args.strict=false Throw if record(s) doesn't exists | ||
* `returns` **{Imm}**: Modified collection | ||
**Example** | ||
```js | ||
collection = collection.remove(id); | ||
collection = collection.remove(arrayOfIds); | ||
``` | ||
### [replace](src/imm.js#L446) | ||
Replaces one item or many. This discards any previous data from the replaced items. If records doesn't exist then it just gets added. This throws if a record doesn't have an key. | ||
* **{Object}**: recordOrRecords Record or records to replace | ||
* **{Object}**: args Optional arguments | ||
* **{Boolean}**: args.strict=false Throws if record exist | ||
* **{Boolean}**: args.requireKey=true Throws if record doesn't have a key | ||
* `returns` **{Imm}**: Modified Imm collection | ||
**Example** | ||
```js | ||
collection = collection.replace(record) | ||
collection = collection.replace(array) | ||
``` | ||
### [update](src/imm.js#L504) | ||
Updates one record or many. This merges the given data with the existing one. If a record is not found then it gets added. This throws if a record doesn't have an key | ||
* **{Object|Array}**: recordOrRecords Record or records to update | ||
* **{Object}**: args Optional arguments | ||
* **{Boolean}**: args.strict=false Throws if record exist | ||
* `returns` **{Imm}**: Modified collection | ||
**Example** | ||
```js | ||
collection = collection.update(record) | ||
collection = collection.update(array) | ||
``` | ||
### Things to note | ||
- Methods that return collections (e.g. filter) return an instance of Imm | ||
- Methods that return one record (e.g. get, find), return a plain JS object | ||
Test | ||
---- | ||
```bash | ||
@@ -252,4 +60,3 @@ npm install | ||
Build | ||
----- | ||
### Build | ||
@@ -262,6 +69,5 @@ This will lint, test, minify and create documentation | ||
Related projects | ||
---------------- | ||
## Related projects | ||
- [immutable.js](https://github.com/facebook/immutable-js) | ||
- [seamless-immutable](https://github.com/rtfeldman/seamless-immutable) |
562
src/imm.js
@@ -0,1 +1,3 @@ | ||
/* @flow */ | ||
//! Imm | ||
@@ -7,557 +9,13 @@ //! Immutable collections | ||
var Immutable = require('seamless-immutable'); | ||
// CommonJS + AMD + Global boilerplate | ||
// https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js | ||
if (Immutable == null) throw new Error('Immutable is null'); | ||
function imm() { | ||
throw new Error('Using imm directly is deprecated, use imm.list instead'); | ||
} | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define(['seamless-immutable'], function (Immutable) { | ||
return (root.imm = factory(Immutable)); | ||
}); | ||
} else if (typeof exports === 'object') { | ||
// Node. Does not work with strict CommonJS, but | ||
// only CommonJS-like enviroments that support module.exports, | ||
// like Node. | ||
var Immutable = require('seamless-immutable'); | ||
module.exports = factory(Immutable); | ||
} else { | ||
// Browser globals | ||
root.imm = factory(root.Immutable); | ||
} | ||
}(this, function (Immutable) { | ||
imm.list = require('./list.js')(Immutable); | ||
imm.obj = require('./obj.js')(Immutable); | ||
"use strict"; | ||
if (Immutable == null) throw new Error('Immutable is null'); | ||
var DEFAULT_KEY = 'id'; | ||
// Utility functions | ||
var _isArray = Array.isArray || function(obj) { | ||
return toString.call(obj) === '[object Array]'; | ||
}; | ||
function _wrapAsArray(recordOrRecords) { | ||
return _isArray(recordOrRecords) ? recordOrRecords : [recordOrRecords]; | ||
} | ||
function _idsAsStrings(array) { | ||
return array.map(function (v) { | ||
return "" + v; | ||
}); | ||
} | ||
function _idsFromRecords(array, key) { | ||
if (!key) throw new Error("Must provide a key"); | ||
return array.map(function (record) { | ||
return record[key]; | ||
}); | ||
} | ||
function _arrayContains(array, value) { | ||
for (var a = 0; a < array.length; a++) { | ||
if (array[a] === value) return true; | ||
} | ||
return false; | ||
} | ||
function _isImmutable(object) { | ||
return Immutable.isImmutable(object); | ||
} | ||
function isObject(obj) { | ||
var type = typeof obj; | ||
return type === 'function' || type === 'object' && !!obj; | ||
} | ||
function _assertIsImmutable(object) { | ||
var is = _isImmutable(object); | ||
if (!is) throw new Error("Not an immutable object"); | ||
} | ||
function _assertIsPlainArray(array) { | ||
var one = !_isArray(array); | ||
var two = _isImmutable(array); | ||
if (one || two) throw new Error("You must provide an array"); | ||
} | ||
function _assertIsObject(object, msg) { | ||
if (!msg) msg = 'Not an object'; | ||
if (!isObject(object)) throw new Error(msg); | ||
} | ||
function _generateUID() { | ||
var d = new Date().getTime(); | ||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
var r = (d + Math.random()*16)%16 | 0; | ||
d = Math.floor(d/16); | ||
return (c=='x' ? r : (r&0x3|0x8)).toString(16); | ||
}); | ||
return uuid; | ||
} | ||
// Fill in a given object with default properties. | ||
// http://underscorejs.org/#defaults | ||
function _defaults(obj) { | ||
obj = obj || {}; | ||
for (var i = 1, length = arguments.length; i < length; i++) { | ||
var source = arguments[i]; | ||
for (var prop in source) { | ||
if (obj[prop] === void 0) obj[prop] = source[prop]; | ||
} | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Returns an Imm collection | ||
* Keys are always sorted in alphabetical order | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var records = [{id: 1, label: 'Sam'}, {...}]; | ||
* collection = imm(records); | ||
* ``` | ||
* imm assumes that the id key is called `id`. You can provide an optional argument: | ||
* | ||
* ```js | ||
* collection = imm(records, {key: '_id'}); | ||
* ``` | ||
* | ||
* @param {Array} records Array of records | ||
* @param {Object} args Optional arguments | ||
* @param {String} args.key=id Optional name of id key e.g. _id | ||
* @return {Imm} Imm collection | ||
* @api public | ||
*/ | ||
function imm(records, args) { | ||
return _wrapPlainArray(records, args); | ||
} | ||
// @param {Array} | ||
// @return {Imm} | ||
function _wrapPlainArray(array, args) { | ||
var id, mergable; | ||
if (!array) array = []; | ||
if (args) _assertIsObject(args, 'You must provide an object for arguments'); | ||
var defaults = { | ||
key: DEFAULT_KEY | ||
}; | ||
args = _defaults(args, defaults); | ||
_assertIsPlainArray(array); | ||
// return a immutable object | ||
var col = Immutable(array).asObject(function (record) { | ||
id = record[args.key]; | ||
if (!id) { | ||
id = _generateUID(); | ||
mergable = {}; | ||
mergable[args.key] = id; | ||
record = record.merge(mergable); | ||
} | ||
return [id, record]; | ||
}); | ||
return _wrapImmutableCollection(col, args); | ||
} | ||
// @param {Immutable} | ||
// @return {Imm} | ||
function _wrapImmutableCollection(immutableCollection, globalArgs) { | ||
_assertIsImmutable(immutableCollection); | ||
function _wrapPlainArrayWithArgs(array) { | ||
return _wrapPlainArray(array, globalArgs); | ||
} | ||
function _wrapImmutableCollectionWithArgs(immutableCollection) { | ||
return _wrapImmutableCollection(immutableCollection, globalArgs); | ||
} | ||
/** | ||
* Adds one or more records. | ||
* If record already exists then it gets replaced. | ||
* If a record doesn't have a key, then the key will be autogenerated. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* // add one | ||
* collection = collection.add(record) | ||
* | ||
* // add many records | ||
* collection = collection.add(array) | ||
* ``` | ||
* | ||
* @param {Object|Array} recordOrRecords Record or records to add | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throw if record already exists | ||
* @return {Imm} modified collection | ||
* @api public | ||
*/ | ||
function add(recordOrRecords, args) { | ||
var defaults = { | ||
strict: false | ||
}; | ||
args = _defaults(args, defaults); | ||
if (args.strict) { | ||
// throw if any record exists | ||
var records = _wrapAsArray(recordOrRecords); | ||
var ids = _idsFromRecords(records, globalArgs.key); | ||
if (anyExist(ids)) throw new Error('Some records already exist'); | ||
} | ||
args.requireKey = false; | ||
return replace(recordOrRecords, args); | ||
} | ||
/** | ||
* Check if the given ID or all given IDs exist. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var exist = allExist(21); | ||
* var exist = allExist([11, 21]); | ||
* ``` | ||
* | ||
* @param {Number|String|Array} idOrIds ID or IDs to check | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
function allExist(idOrIds) { | ||
var ids = _wrapAsArray(idOrIds); | ||
for (var a = 0; a < ids.length; a++) { | ||
var id = ids[a]; | ||
if (!immutableCollection[id]) return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Check if the given ID or any given IDs exist | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var exist = anyExist(21); | ||
* var exist = anyExist([11, 21]); | ||
* ``` | ||
* | ||
* @param {Number|String|Array} idOrIds Id or Ids to check | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
function anyExist(idOrIds) { | ||
var ids = _wrapAsArray(idOrIds); | ||
for (var a = 0; a < ids.length; a++) { | ||
var id = ids[a]; | ||
if (immutableCollection[id]) return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Get all records. | ||
* Records in the array are plain mutable JS objects. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var records = collection.array(); | ||
* ``` | ||
* | ||
* @return {Array} records Plain array with records | ||
* @api public | ||
*/ | ||
function array() { | ||
return asPlainArray(); | ||
} | ||
function asPlainArray() { | ||
return Object.keys(immutableCollection).map(function (key) { | ||
return immutableCollection[key].asMutable({deep: true}); | ||
}); | ||
} | ||
/** | ||
* Records count. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* count = collection.count(); | ||
* ``` | ||
* | ||
* @return {Number} count | ||
* @api public | ||
*/ | ||
function count(){ | ||
return Object.keys(immutableCollection).length; | ||
} | ||
/** | ||
* Filters the collection based on a filtering function. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.filter(function (record) { | ||
* return record.age > 18; | ||
* }); | ||
* ``` | ||
* | ||
* @param {Function} filterer Filtering function | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function filter(filterer) { | ||
var newCol = asPlainArray(); | ||
newCol = newCol.filter(filterer); | ||
return _wrapPlainArrayWithArgs(newCol); | ||
} | ||
/** | ||
* Finds one record. | ||
* Returns a plain JS mutable object. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var record = collection.find(function (record) { | ||
* return record.age === 18; | ||
* }); | ||
* ``` | ||
* | ||
* @param {Function} finder Finder function | ||
* @return {Object} record Record or undefined | ||
* @api public | ||
*/ | ||
function find(finder) { | ||
var records = asPlainArray(); | ||
for (var a = 0; a < records.length; a++) { | ||
var record = records[a]; | ||
if (finder(record)) { | ||
return record; | ||
} | ||
} | ||
return void(0); | ||
} | ||
/** | ||
* Get a record. | ||
* Returned record is a plain JS mutable object. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* var record = collection.get(11) | ||
* var record = collection.get('11') // same as 11 | ||
* ``` | ||
* | ||
* @param {Number|String} id Id to fetch | ||
* @return {Object} record | ||
* @api public | ||
*/ | ||
function get(id) { | ||
var record = immutableCollection[id]; | ||
if (!record) return void(0); | ||
return record.asMutable({deep: true}); | ||
} | ||
/** | ||
* Map the collection through a given function | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.map(function (record) { | ||
* return {foo: record.id}; | ||
* }); | ||
* ``` | ||
* | ||
* @param {Function} mapper Mapping function | ||
* @return {Array} array | ||
* @api public | ||
*/ | ||
function map(mapper) { | ||
var newCol = asPlainArray(); | ||
return newCol.map(mapper); | ||
} | ||
/** | ||
* Removes one or many records based on the id. | ||
* If record is not found then it just gets skipped. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.remove(id); | ||
* collection = collection.remove(arrayOfIds); | ||
* ``` | ||
* | ||
* @param {Number|String|Array} idOrIds Id or ids to remove | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throw if record(s) doesn't exists | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function remove(idOrIds, args) { | ||
var ids = _wrapAsArray(idOrIds); | ||
// ids need to be strings for without | ||
ids = _idsAsStrings(ids); | ||
if (args && args.strict) { | ||
if (!allExist(ids)) throw new Error('Some records do not exist'); | ||
} | ||
var newCol = immutableCollection.without(ids); | ||
return _wrapImmutableCollectionWithArgs(newCol); | ||
} | ||
/** | ||
* Replaces one item or many. | ||
* This discards any previous data from the replaced items. | ||
* If records doesn't exist then it just gets added. | ||
* This throws if a record doesn't have an key. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.replace(record) | ||
* collection = collection.replace(array) | ||
* ``` | ||
* | ||
* @param {Object} recordOrRecords Record or records to replace | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throws if record exist | ||
* @param {Boolean} args.requireKey=true Throws if record doesn't have a key | ||
* @return {Imm} Modified Imm collection | ||
* @api public | ||
*/ | ||
function replace(recordOrRecords, args) { | ||
var record, id; | ||
var records = _wrapAsArray(recordOrRecords); | ||
var ids = _idsFromRecords(records, globalArgs.key); | ||
ids = _idsAsStrings(ids); | ||
var newCol = immutableCollection.without(ids); | ||
var merges = {}; | ||
var defaults = { | ||
strict: false, | ||
requireKey: true | ||
}; | ||
args = _defaults(args, defaults); | ||
if (args.strict) { | ||
// throw if any record exists | ||
if (anyExist(ids)) throw new Error('Some records already exist'); | ||
} | ||
for (var a = 0; a < records.length; a++) { | ||
record = records[a]; | ||
id = record[globalArgs.key]; | ||
if (!id) { | ||
if (args.requireKey) throw new Error("Record must have ." + globalArgs.key); | ||
id = _generateUID(); | ||
record[globalArgs.key] = id; | ||
} | ||
if (!id) throw new Error("Record must have ." + globalArgs.key); | ||
merges[id] = record; | ||
} | ||
newCol = newCol.merge(merges); | ||
return _wrapImmutableCollectionWithArgs(newCol); | ||
} | ||
function toImmutable() { | ||
return immutableCollection; | ||
} | ||
/** | ||
* Updates one record or many. | ||
* This merges the given data with the existing one. | ||
* If a record is not found then it gets added. | ||
* This throws if a record doesn't have an key | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* collection = collection.update(record) | ||
* collection = collection.update(array) | ||
* ``` | ||
* | ||
* @param {Object|Array} recordOrRecords Record or records to update | ||
* @param {Object} args Optional arguments | ||
* @param {Boolean} args.strict=false Throws if record exist | ||
* @return {Imm} Modified collection | ||
* @api public | ||
*/ | ||
function update(recordOrRecords, args) { | ||
var defaults = { | ||
strict: false | ||
}; | ||
args = _defaults(args, defaults); | ||
var givenId, givenRecord, toMerge, existing, mergedRecord; | ||
var givenRecords = _wrapAsArray(recordOrRecords); | ||
var newCol = immutableCollection; | ||
if (args.strict) { | ||
// throw if any record exists | ||
var records = _wrapAsArray(recordOrRecords); | ||
var ids = _idsFromRecords(records, globalArgs.key); | ||
if (anyExist(ids)) throw new Error('Some records already exist'); | ||
} | ||
for (var a = 0; a < givenRecords.length; a++) { | ||
givenRecord = givenRecords[a]; | ||
givenId = givenRecord[globalArgs.key]; | ||
// throw if no givenId | ||
if (!givenId) throw new Error('Record must have .' + globalArgs.key); | ||
existing = immutableCollection[givenId]; | ||
if (existing) { | ||
mergedRecord = existing.merge(givenRecord); | ||
} else { | ||
mergedRecord = givenRecord; | ||
} | ||
toMerge = {}; | ||
toMerge[givenId] = mergedRecord; | ||
newCol = newCol.merge(toMerge); | ||
} | ||
return _wrapImmutableCollectionWithArgs(newCol); | ||
} | ||
return { | ||
isImm: true, | ||
add: add, | ||
allExist: allExist, | ||
anyExist: anyExist, | ||
array: array, | ||
count: count, | ||
filter: filter, | ||
find: find, | ||
get: get, | ||
replace: replace, | ||
map: map, | ||
remove: remove, | ||
toImmutable: toImmutable, | ||
update: update, | ||
}; | ||
} | ||
return imm; | ||
})); | ||
module.exports = imm; |
@@ -1,2 +0,2 @@ | ||
module.exports = function () { | ||
module.exports = function() { | ||
return [ | ||
@@ -11,3 +11,3 @@ { | ||
} | ||
]; | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
module.exports = function () { | ||
module.exports = function() { | ||
return [ | ||
@@ -11,3 +11,3 @@ { | ||
} | ||
]; | ||
] | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
90944
65
1756
21
68
1
1