Socket
Socket
Sign inDemoInstall

mixing

Package Overview
Dependencies
0
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.2 to 1.2.3

index.d.ts

2

bower.json
{
"name": "mixing",
"main": ["dist/mixing.js", "dist/mixing.min.js"],
"version": "1.2.2",
"version": "1.2.3",
"homepage": "https://github.com/gamtiq/mixing",

@@ -6,0 +6,0 @@ "authors": [

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

"description": "Functions to mix, filter, change and copy/clone objects",
"version": "1.2.2",
"version": "1.2.3",
"keywords": [

@@ -8,0 +8,0 @@ "mix",

@@ -1,4 +0,4 @@

(function(root, factory) {
if(typeof exports === 'object') {
if (root === undefined && window !== undefined) root = window;
if (typeof module === 'object' && module.exports) {
module.exports = factory(require, exports, module);

@@ -16,549 +16,572 @@ }

}(this, function(require, exports, module) {
/**
* @module mixing
*/
/**
* @module mixing
*/
/*jshint latedef:nofunc*/
/*jshint latedef:nofunc*/
var defaultSettings;
var defaultSettings;
if (! Array.isArray) {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
};
}
if (! Array.isArray) {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
};
}
function prepareFieldList(fieldList, value) {
var map, nI, regexp, sType;
if (Array.isArray(fieldList)) {
if (fieldList.length > 0) {
map = {};
nI = fieldList.length;
do {
map[ fieldList[--nI] ] = value || null;
} while(nI);
}
function prepareFieldList(fieldList, value) {
var map, nI, regexp, sType;
if (Array.isArray(fieldList)) {
if (fieldList.length > 0) {
map = {};
nI = fieldList.length;
do {
map[ fieldList[--nI] ] = value || null;
} while(nI);
}
else {
sType = typeof fieldList;
if (sType === "string" || sType === "symbol") {
map = {};
map[fieldList] = value || null;
}
else {
sType = typeof fieldList;
if (sType === "string" || sType === "symbol") {
map = {};
map[fieldList] = value || null;
}
else if (sType === "object") {
if (fieldList instanceof RegExp) {
regexp = fieldList;
}
else if (sType === "object") {
if (fieldList instanceof RegExp) {
regexp = fieldList;
}
else {
map = fieldList;
}
else {
map = fieldList;
}
}
return {map: map, regexp: regexp};
}
return {map: map, regexp: regexp};
}
function copy(destination, source, propName, settings) {
/*jshint laxbreak:true*/
var propValue = source[propName],
sPropString = propName.toString(),
bFuncProp, change, otherNameMap, sType, value;
if ((! settings.ownProperty || source.hasOwnProperty(propName))
&& (! settings.copyMap || (propName in settings.copyMap))
&& (! settings.copyRegExp || settings.copyRegExp.test(sPropString))
&& (! settings.exceptions || ! settings.exceptions[propName])
&& (! settings.exceptRegExp || ! settings.exceptRegExp.test(sPropString))
&& (! settings.filter || settings.filter.call(null, {field: propName, value: propValue, target: destination, source: source}))
&& (! settings.filterRegExp || settings.filterRegExp.test(typeof propValue === "symbol" ? propValue.toString() : propValue))) {
function copy(destination, source, propName, settings) {
/*jshint laxbreak:true*/
var propValue = source[propName],
sPropString = propName.toString(),
bFuncProp, change, otherNameMap, sType, value;
if ((! settings.ownProperty || source.hasOwnProperty(propName))
&& (! settings.copyMap || (propName in settings.copyMap))
&& (! settings.copyRegExp || settings.copyRegExp.test(sPropString))
&& (! settings.exceptions || ! settings.exceptions[propName])
&& (! settings.exceptRegExp || ! settings.exceptRegExp.test(sPropString))
&& (! settings.filter || settings.filter.call(null, {field: propName, value: propValue, target: destination, source: source}))
/* jshint -W122 */
&& (! settings.filterRegExp || settings.filterRegExp.test(typeof propValue === "symbol" ? propValue.toString() : propValue))) {
/* jshint +W122 */
otherNameMap = settings.otherNameMap;
if (otherNameMap && (propName in otherNameMap)) {
propName = otherNameMap[propName];
}
sType = typeof propValue;
// If recursive mode and field's value is an object
if (settings.recursive && propValue && sType === "object" && (value = destination[propName]) && typeof value === "object"
&& (! Array.isArray(propValue) || settings.mixFromArray) && (! Array.isArray(value) || settings.mixToArray)) {
mixing(value, propValue,
settings.mixFromArray
? mixing({oneSource: true}, settings)
: settings);
}
else {
bFuncProp = (sType === "function");
if ((settings.overwrite || ! (propName in destination))
&& (! bFuncProp || settings.copyFunc)) {
if (settings.changeFunc) {
propValue = settings.changeFunc.call(null, {field: propName, value: propValue, target: destination, source: source});
}
else if ((change = settings.change) && (propName in change)) {
propValue = change[propName];
}
if (bFuncProp && settings.funcToProto) {
destination.constructor.prototype[propName] = propValue;
}
else {
destination[propName] = propValue;
}
if (otherNameMap && (propName in otherNameMap)) {
propName = otherNameMap[propName];
}
sType = typeof propValue;
// If recursive mode and field's value is an object
if (settings.recursive && propValue && sType === "object" && (value = destination[propName]) && typeof value === "object"
&& (! Array.isArray(propValue) || settings.mixFromArray) && (! Array.isArray(value) || settings.mixToArray)) {
mixing(value, propValue,
settings.mixFromArray
? mixing({oneSource: true}, settings)
: settings);
}
else {
bFuncProp = (sType === "function");
if ((settings.overwrite || ! (propName in destination))
&& (! bFuncProp || settings.copyFunc)) {
if (settings.changeFunc) {
propValue = settings.changeFunc.call(null, {field: propName, value: propValue, target: destination, source: source});
}
else if ((change = settings.change) && (propName in change)) {
propValue = change[propName];
}
if (bFuncProp && settings.funcToProto) {
destination.constructor.prototype[propName] = propValue;
}
else {
destination[propName] = propValue;
}
}
}
}
}
/**
* Copy/add all fields and functions from source objects into the target object.
* As a result the target object may be modified.
*
* @param {Object | Function} destination
* The target object into which fields and functions will be copied.
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* If a source is a falsy value (e.g. <code>null</code> or <code>undefined</code>), the source will be skipped.
* @param {Object} [settings]
* Operation settings. Fields are names of settings, their values are the corresponding values of settings.
* The following settings are being supported.
* <table>
* <tr>
* <th>Name</th><th>Type</th><th>Default value</th><th>Description</th>
* </tr>
* <tr>
* <td><code>copyFunc</code></td>
* <td><code>Boolean</code></td>
* <td><code>true</code></td>
* <td>Should functions be copied?</td>
* </tr>
* <tr>
* <td><code>funcToProto</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should functions be copied into <code>prototype</code> of the target object's <code>constructor</code>
* (i.e. into <code>destination.constructor.prototype</code>)?
* <br>
* If <code>false</code> then functions will be copied directly into the target object.
* </td>
* </tr>
* <tr>
* <td><code>processSymbol</code></td>
* <td><code>Boolean</code></td>
* <td><code>true</code></td>
* <td>Should symbol property keys (i.e. fields whose name is a symbol) be processed?</td>
* </tr>
* <tr>
* <td><code>overwrite</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>Should a field/function be overwritten when it exists in the target object?</td>
* </tr>
* <tr>
* <td><code>recursive</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should this function be called recursively when field's value of the target and source object is an object?
* <br>
* If <code>true</code> then object fields from the target and source objects will be mixed by using this function
* with the same settings.
* <br>
* This option has no dependency with <code>overwrite</code> setting and has priority over it.
* </td>
* </tr>
* <tr>
* <td><code>mixFromArray</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should contents of a field of the source object be copied when the field's value is an array?
* <br>
* Will be used only when <code>recursive</code> setting has <code>true</code> value.
* </td>
* </tr>
* <tr>
* <td><code>mixToArray</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should contents of a field of the source object be copied into a field of the target object
* when the latest field's value is an array?
* <br>
* Will be used only when <code>recursive</code> setting has <code>true</code> value.
* </td>
* </tr>
* <tr>
* <td><code>oneSource</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Indicates that array that is passed as <code>source</code> parameter should be interpreted
* directly as copied object instead of list of source objects.
* </td>
* </tr>
* <tr>
* <td><code>ownProperty</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>Should only own properties of the source object be copied in the target object?</td>
* </tr>
* <tr>
* <td><code>copy</code></td>
* <td><code>Array | Object | RegExp | String | Symbol</code></td>
* <td><code>""</code> (empty string)</td>
* <td>
* Array, object, regular expression or string/symbol that defines names of fields/functions that should be copied.
* <br>
* If an object is passed then his fields determine copied elements.
* If a regular expression is passed, then field names matching the regular expression will be copied.
* If a string/symbol is passed then it is name of the only copied field.
* </td>
* </tr>
* <tr>
* <td><code>except</code></td>
* <td><code>Array | Object | RegExp | String | Symbol</code></td>
* <td><code>""</code> (empty string)</td>
* <td>
* Array, object, regular expression or string/symbol that defines names of fields/functions that shouldn't be copied.
* <br>
* If an object is passed then his fields with true values determine non-copied elements.
* If a regular expression is passed, then field names matching the regular expression will not be copied.
* If a string/symbol is passed then it is name of the only non-copied field.
* </td>
* </tr>
* <tr>
* <td><code>filter</code></td>
* <td><code>Function | RegExp</code></td>
* <td><code>null</code></td>
* <td>
* Function or regular expression that can be used to select elements that should be copied.
* <br>
* If regular expression is passed, only those fields will be copied whose values are matching regular expression.
* <br>
* If specified function returns <code>true</code> for a field,
* the field will be copied in the target object.
* <br>
* An object having the following fields is passed into filter function:
* <ul>
* <li><code>field</code> - field name
* <li><code>value</code> - field value
* <li><code>target</code> - reference to the target object
* <li><code>source</code> - reference to the source object
* </ul>
* </td>
* </tr>
* <tr>
* <td><code>otherName</code></td>
* <td><code>Object</code></td>
* <td><code>null</code></td>
* <td>
* Defines "renaming table" for copied elements.
* <br>
* Fields of the table are names from a source object, values are the corresponding names in the target object.
* <br>
* For example, the call
* <br>
* <code>
* mixing({}, {field: 1, func: "no-func"}, {otherName: {"field": "prop", "func": "method"}})
* </code>
* <br>
* will return the following object
* <br>
* <code>{prop: 1, method: "no-func"}</code>
* </td>
* </tr>
* <tr>
* <td><code>change</code></td>
* <td><code>Function | Object</code></td>
* <td><code>null</code></td>
* <td>
* Function or object that gives ability to change values that should be copied.
* <br>
* If an object is passed then his fields determine new values for copied elements.
* <br>
* If a function is passed then value returned by the function for a field will be copied into the target object
* instead of original field's value.
* <br>
* An object having the following fields is passed into change function:
* <ul>
* <li><code>field</code> - field name
* <li><code>value</code> - field value
* <li><code>target</code> - reference to the target object
* <li><code>source</code> - reference to the source object
* </ul>
* </td>
* </tr>
* </table>
* Default values of settings can be redefined by {@link module:mixing.setSettings setSettings} method.
* <br>
* <code>copy</code>, <code>except</code> and <code>filter</code> settings can be used together.
* In such situation a field will be copied only when the field satisfies to all settings
* (i.e. belongs to copied elements, not in exceptions and conforms to filter).
* @return {Object}
* Modified target object.
* @alias module:mixing
*/
function mixing(destination, source, settings) {
/*jshint boss:true, laxbreak:true*/
var destinationType = typeof destination;
var sourceType = typeof source;
if (destination && (destinationType === "object" || destinationType === "function")
&& source && (sourceType === "object" || sourceType === "function")) {
var obj;
// Prepare parameters
if (typeof settings !== "object" || settings === null) {
settings = defaultSettings || {};
}
else if (defaultSettings) {
obj = defaultSettings;
defaultSettings = null;
settings = mixing({}, [settings, obj]);
defaultSettings = obj;
}
if (! Array.isArray(source) || settings.oneSource) {
source = [source];
}
// Prepare settings
var getOwnPropertySymbols = Object.getOwnPropertySymbols,
getPrototypeOf = Object.getPrototypeOf,
options = {
copyFunc: ("copyFunc" in settings ? settings.copyFunc : true),
funcToProto: Boolean(settings.funcToProto),
processSymbol: ("processSymbol" in settings ? settings.processSymbol : true)
&& typeof getOwnPropertySymbols === "function",
mixFromArray: Boolean(settings.mixFromArray),
mixToArray: Boolean(settings.mixToArray),
overwrite: Boolean(settings.overwrite),
ownProperty: Boolean(settings.ownProperty),
recursive: Boolean(settings.recursive),
otherNameMap: ("otherName" in settings ? settings.otherName : null)
},
bOwnProperty = options.ownProperty,
bProcessSymbol = options.processSymbol,
change = settings.change,
copyList = settings.copy,
exceptList = settings.except,
filter = settings.filter,
nI, nK, nL, nQ, propName;
if (copyList) {
copyList = prepareFieldList(copyList);
options.copyMap = copyList.map;
options.copyRegExp = copyList.regexp;
}
if (exceptList) {
exceptList = prepareFieldList(exceptList, true);
options.exceptions = exceptList.map;
options.exceptRegExp = exceptList.regexp;
}
if (filter) {
options[typeof filter === "object" ? "filterRegExp" : "filter"] = filter;
}
if (change) {
options[typeof change === "function" ? "changeFunc" : "change"] = change;
}
// Copy fields and functions according to settings
for (nI = 0, nL = source.length; nI < nL; nI++) {
if (obj = source[nI]) {
for (propName in obj) {
copy(destination, obj, propName, options);
}
// Process symbol property keys
if (bProcessSymbol) {
exceptList = {};
do {
copyList = getOwnPropertySymbols(obj);
for (nK = 0, nQ = copyList.length; nK < nQ; nK++) {
propName = copyList[nK];
if (! (propName in exceptList)) {
copy(destination, obj, propName, options);
exceptList[propName] = true;
}
/**
* Copy/add all fields and functions from source objects into the target object.
* As a result the target object may be modified.
*
* @param {Object | Function} destination
* The target object into which fields and functions will be copied.
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* If a source is a falsy value (e.g. <code>null</code> or <code>undefined</code>), the source will be skipped.
* @param {Object} [settings]
* Operation settings. Fields are names of settings, their values are the corresponding values of settings.
* The following settings are being supported.
* <table>
* <tr>
* <th>Name</th><th>Type</th><th>Default value</th><th>Description</th>
* </tr>
* <tr>
* <td><code>copyFunc</code></td>
* <td><code>Boolean</code></td>
* <td><code>true</code></td>
* <td>Should functions be copied?</td>
* </tr>
* <tr>
* <td><code>funcToProto</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should functions be copied into <code>prototype</code> of the target object's <code>constructor</code>
* (i.e. into <code>destination.constructor.prototype</code>)?
* <br>
* If <code>false</code> then functions will be copied directly into the target object.
* </td>
* </tr>
* <tr>
* <td><code>processSymbol</code></td>
* <td><code>Boolean</code></td>
* <td><code>true</code></td>
* <td>Should symbol property keys (i.e. fields whose name is a symbol) be processed?</td>
* </tr>
* <tr>
* <td><code>overwrite</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>Should a field/function be overwritten when it exists in the target object?</td>
* </tr>
* <tr>
* <td><code>recursive</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should this function be called recursively when field's value of the target and source object is an object?
* <br>
* If <code>true</code> then object fields from the target and source objects will be mixed by using this function
* with the same settings.
* <br>
* This option has no dependency with <code>overwrite</code> setting and has priority over it.
* </td>
* </tr>
* <tr>
* <td><code>mixFromArray</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should contents of a field of the source object be copied when the field's value is an array?
* <br>
* Will be used only when <code>recursive</code> setting has <code>true</code> value.
* </td>
* </tr>
* <tr>
* <td><code>mixToArray</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Should contents of a field of the source object be copied into a field of the target object
* when the latest field's value is an array?
* <br>
* Will be used only when <code>recursive</code> setting has <code>true</code> value.
* </td>
* </tr>
* <tr>
* <td><code>oneSource</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>
* Indicates that array that is passed as <code>source</code> parameter should be interpreted
* directly as copied object instead of list of source objects.
* </td>
* </tr>
* <tr>
* <td><code>ownProperty</code></td>
* <td><code>Boolean</code></td>
* <td><code>false</code></td>
* <td>Should only own properties of the source object be copied in the target object?</td>
* </tr>
* <tr>
* <td><code>copy</code></td>
* <td><code>Array | Object | RegExp | String | Symbol</code></td>
* <td><code>""</code> (empty string)</td>
* <td>
* Array, object, regular expression or string/symbol that defines names of fields/functions that should be copied.
* <br>
* If an object is passed then his fields determine copied elements.
* If a regular expression is passed, then field names matching the regular expression will be copied.
* If a string/symbol is passed then it is name of the only copied field.
* </td>
* </tr>
* <tr>
* <td><code>except</code></td>
* <td><code>Array | Object | RegExp | String | Symbol</code></td>
* <td><code>""</code> (empty string)</td>
* <td>
* Array, object, regular expression or string/symbol that defines names of fields/functions that shouldn't be copied.
* <br>
* If an object is passed then his fields with true values determine non-copied elements.
* If a regular expression is passed, then field names matching the regular expression will not be copied.
* If a string/symbol is passed then it is name of the only non-copied field.
* </td>
* </tr>
* <tr>
* <td><code>filter</code></td>
* <td><code>Function | RegExp</code></td>
* <td><code>null</code></td>
* <td>
* Function or regular expression that can be used to select elements that should be copied.
* <br>
* If regular expression is passed, only those fields will be copied whose values are matching regular expression.
* <br>
* If specified function returns <code>true</code> for a field,
* the field will be copied in the target object.
* <br>
* An object having the following fields is passed into filter function:
* <ul>
* <li><code>field</code> - field name
* <li><code>value</code> - field value
* <li><code>target</code> - reference to the target object
* <li><code>source</code> - reference to the source object
* </ul>
* </td>
* </tr>
* <tr>
* <td><code>otherName</code></td>
* <td><code>Object</code></td>
* <td><code>null</code></td>
* <td>
* Defines "renaming table" for copied elements.
* <br>
* Fields of the table are names from a source object, values are the corresponding names in the target object.
* <br>
* For example, the call
* <br>
* <code>
* mixing({}, {field: 1, func: "no-func"}, {otherName: {"field": "prop", "func": "method"}})
* </code>
* <br>
* will return the following object
* <br>
* <code>{prop: 1, method: "no-func"}</code>
* </td>
* </tr>
* <tr>
* <td><code>change</code></td>
* <td><code>Function | Object</code></td>
* <td><code>null</code></td>
* <td>
* Function or object that gives ability to change values that should be copied.
* <br>
* If an object is passed then his fields determine new values for copied elements.
* <br>
* If a function is passed then value returned by the function for a field will be copied into the target object
* instead of original field's value.
* <br>
* An object having the following fields is passed into change function:
* <ul>
* <li><code>field</code> - field name
* <li><code>value</code> - field value
* <li><code>target</code> - reference to the target object
* <li><code>source</code> - reference to the source object
* </ul>
* </td>
* </tr>
* </table>
* Default values of settings can be redefined by {@link module:mixing.setSettings setSettings} method.
* <br>
* <code>copy</code>, <code>except</code> and <code>filter</code> settings can be used together.
* In such situation a field will be copied only when the field satisfies to all settings
* (i.e. belongs to copied elements, not in exceptions and conforms to filter).
* @return {Object}
* Modified target object.
* @alias module:mixing
*/
function mixing(destination, source, settings) {
/*jshint boss:true, laxbreak:true*/
var destinationType = typeof destination;
var sourceType = typeof source;
if (destination && (destinationType === "object" || destinationType === "function")
&& source && (sourceType === "object" || sourceType === "function")) {
var obj;
// Prepare parameters
if (typeof settings !== "object" || settings === null) {
settings = defaultSettings || {};
}
else if (defaultSettings) {
obj = defaultSettings;
defaultSettings = null;
settings = mixing({}, [settings, obj]);
defaultSettings = obj;
}
if (! Array.isArray(source) || settings.oneSource) {
source = [source];
}
// Prepare settings
var getOwnPropertySymbols = Object.getOwnPropertySymbols,
getPrototypeOf = Object.getPrototypeOf,
options = {
copyFunc: ("copyFunc" in settings ? settings.copyFunc : true),
funcToProto: Boolean(settings.funcToProto),
processSymbol: ("processSymbol" in settings ? settings.processSymbol : true)
&& typeof getOwnPropertySymbols === "function",
mixFromArray: Boolean(settings.mixFromArray),
mixToArray: Boolean(settings.mixToArray),
overwrite: Boolean(settings.overwrite),
ownProperty: Boolean(settings.ownProperty),
recursive: Boolean(settings.recursive),
otherNameMap: ("otherName" in settings ? settings.otherName : null)
},
bOwnProperty = options.ownProperty,
bProcessSymbol = options.processSymbol,
change = settings.change,
copyList = settings.copy,
exceptList = settings.except,
filter = settings.filter,
nI, nK, nL, nQ, propName;
if (copyList) {
copyList = prepareFieldList(copyList);
options.copyMap = copyList.map;
options.copyRegExp = copyList.regexp;
}
if (exceptList) {
exceptList = prepareFieldList(exceptList, true);
options.exceptions = exceptList.map;
options.exceptRegExp = exceptList.regexp;
}
if (filter) {
options[typeof filter === "object" ? "filterRegExp" : "filter"] = filter;
}
if (change) {
options[typeof change === "function" ? "changeFunc" : "change"] = change;
}
// Copy fields and functions according to settings
for (nI = 0, nL = source.length; nI < nL; nI++) {
if (obj = source[nI]) {
for (propName in obj) {
copy(destination, obj, propName, options);
}
// Process symbol property keys
if (bProcessSymbol) {
exceptList = {};
do {
copyList = getOwnPropertySymbols(obj);
for (nK = 0, nQ = copyList.length; nK < nQ; nK++) {
propName = copyList[nK];
if (! (propName in exceptList)) {
copy(destination, obj, propName, options);
exceptList[propName] = true;
}
obj = bOwnProperty
? null
: getPrototypeOf(obj);
} while (obj);
}
}
obj = bOwnProperty
? null
: getPrototypeOf(obj);
} while (obj);
}
}
}
return destination;
}
return destination;
}
/**
* Change values of fields of given object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing(source, source, {change: change, overwrite: true, oneSource: true});
* </pre></code>
*
* @param {Array | Object} source
* An array or an object whose fields should be modified.
* @param {Function | Object} change
* A function or an object that specifies the modification. See {@link module:mixing mixing} for details.
* @return {Object}
* Modified <code>source</code> object.
*/
mixing.change = function(source, change) {
return mixing(source, source, {change: change, overwrite: true, oneSource: true});
};
/**
* Copy values of all of the own properties from one or more source objects to the target object
* (similar to <code>Object.assign</code>).
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing(destination, Array.prototype.slice.call(arguments, 1), {overwrite: true, ownProperty: true});
* </pre></code>
*
* @param {Object | Function} destination
* The target object into which fields and functions will be copied.
* @param {...Object} source
* An object whose contents will be copied.
* If a source is a falsy value (e.g. <code>null</code> or <code>undefined</code>), the source will be skipped.
* @return {Object}
* Modified <code>target</code> object.
*/
mixing.assign = function(destination) {
return mixing(destination, Array.prototype.slice.call(arguments, 1), {overwrite: true, ownProperty: true});
};
/**
* Make a copy of source object(s).
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var copy = mixing({}, source, settings);
* </pre></code>
*
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* @param {Object} [settings]
* Operation settings. See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing contents of source objects.
*/
mixing.copy = function(source, settings) {
return mixing({}, source, settings);
};
/**
* Change values of fields of given object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing(source, source, {change: change, overwrite: true, oneSource: true});
* </pre></code>
*
* @param {Array | Object} source
* An array or an object whose fields should be modified.
* @param {Function | Object} change
* A function or an object that specifies the modification. See {@link module:mixing mixing} for details.
* @return {Object}
* Modified <code>source</code> object.
*/
mixing.change = function(source, change) {
return mixing(source, source, {change: change, overwrite: true, oneSource: true});
};
/**
* Copy fields from source object(s) into every object item of given array.
*
* @param {Array} destinationList
* An array whose items should be modified.
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* @param {Object} [settings]
* Operation settings that will be applied to every copying. See {@link module:mixing mixing} for details.
* @return {Array}
* Original <code>destinationList</code> with possibly modified object items.
*/
mixing.mixToItems = function(destinationList, source, settings) {
for (var nI = 0, nL = destinationList.length; nI < nL; nI++) {
destinationList[nI] = mixing(destinationList[nI], source, settings);
}
return destinationList;
};
/**
* Make a copy of source object(s).
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var copy = mixing({}, source, settings);
* </pre></code>
*
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* @param {Object} [settings]
* Operation settings. See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing contents of source objects.
*/
mixing.copy = function(source, settings) {
return mixing({}, source, settings);
};
/**
* Make a copy of <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var copy = mixing({}, this, settings);
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Object} [settings]
* Operation settings. See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing contents of <code>this</code> object.
*/
mixing.clone = function(settings) {
return mixing({}, this, settings);
};
/**
* Copy fields from source object(s) into every object item of given array.
*
* @param {Array} destinationList
* An array whose items should be modified.
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* @param {Object} [settings]
* Operation settings that will be applied to every copying. See {@link module:mixing mixing} for details.
* @return {Array}
* Original <code>destinationList</code> with possibly modified object items.
*/
mixing.mixToItems = function(destinationList, source, settings) {
for (var nI = 0, nL = destinationList.length; nI < nL; nI++) {
destinationList[nI] = mixing(destinationList[nI], source, settings);
}
return destinationList;
};
/**
* Filter <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var result = mixing({}, this, {filter: filter});
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Function | Object} filter
* Filter function to select fields or object that represents operation settings including filter function.
* See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing fields of <code>this</code> object for which filter function returns true.
*/
mixing.filter = function(filter) {
return mixing({}, this, typeof filter === "function" ? {filter: filter} : filter);
};
/**
* Make a copy of <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var copy = mixing({}, this, settings);
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Object} [settings]
* Operation settings. See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing contents of <code>this</code> object.
*/
mixing.clone = function(settings) {
return mixing({}, this, settings);
};
/**
* Copy and change values of fields of <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var result = mixing({}, this, {change: change});
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Function | Object} change
* Function to change values of copied fields or object that represents operation settings including change function.
* See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing fields of <code>this</code> object with changed values.
*/
mixing.map = function(change) {
return mixing({}, this, typeof change === "function" ? {change: change} : change);
};
/**
* Filter <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var result = mixing({}, this, {filter: filter});
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Function | Object} filter
* Filter function to select fields or object that represents operation settings including filter function.
* See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing fields of <code>this</code> object for which filter function returns true.
*/
mixing.filter = function(filter) {
return mixing({}, this, typeof filter === "function" ? {filter: filter} : filter);
};
/**
* Copy/add all fields and functions from source objects into <code>this</code> object.
* As a result <code>this</code> object may be modified.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing(this, source, settings);
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* @param {Object} [settings]
* Operation settings. See {@link module:mixing mixing} for details.
* @return {Object}
* Modified <code>this</code> object.
*/
mixing.mix = function(source, settings) {
return mixing(this, source, settings);
};
/**
* Copy and change values of fields of <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* var result = mixing({}, this, {change: change});
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Function | Object} change
* Function to change values of copied fields or object that represents operation settings including change function.
* See {@link module:mixing mixing} for details.
* @return {Object}
* Newly created object containing fields of <code>this</code> object with changed values.
*/
mixing.map = function(change) {
return mixing({}, this, typeof change === "function" ? {change: change} : change);
};
/**
* Change values of fields of <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing.change(this, change);
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Function | Object} change
* A function or an object that specifies the modification. See {@link module:mixing mixing} for details.
* @return {Object}
* Modified <code>this</code> object.
*/
mixing.update = function(change) {
return mixing.change(this, change);
};
/**
* Copy/add all fields and functions from source objects into <code>this</code> object.
* As a result <code>this</code> object may be modified.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing(this, source, settings);
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Array | Object} source
* Array of source objects or just one object whose contents will be copied.
* @param {Object} [settings]
* Operation settings. See {@link module:mixing mixing} for details.
* @return {Object}
* Modified <code>this</code> object.
*/
mixing.mix = function(source, settings) {
return mixing(this, source, settings);
};
/**
* Return default settings that were set earlier.
*
* @return {Object | undefined}
* Default settings that were set earlier or <code>undefined / null</code> if default settings were not set.
*/
mixing.getSettings = function() {
return defaultSettings;
};
/**
* Change values of fields of <code>this</code> object.
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing.change(this, change);
* </pre></code>
* It can be transferred to an object to use as a method.
*
* @param {Function | Object} change
* A function or an object that specifies the modification. See {@link module:mixing mixing} for details.
* @return {Object}
* Modified <code>this</code> object.
*/
mixing.update = function(change) {
return mixing.change(this, change);
};
/**
* Set (redefine, reset) default settings that should be used for subsequent {@link module:mixing mixing} calls.
*
* @param {Object | undefined} [settings]
* Default settings that should be used for subsequent {@link module:mixing mixing} calls.
* Initial default values will be used for settings that are not specified in the passed object.
* Pass <code>undefined</code>, <code>null</code>, non-object or to call without parameter
* to reset default settings to initial values.
* @alias module:mixing.setSettings
*/
mixing.setSettings = function(settings) {
defaultSettings = typeof settings === "object" ? settings : null;
};
/**
* Return default settings that were set earlier.
*
* @return {Object | undefined}
* Default settings that were set earlier or <code>undefined / null</code> if default settings were not set.
*/
mixing.getSettings = function() {
return defaultSettings;
};
module.exports = mixing;
/**
* Set (redefine, reset) default settings that should be used for subsequent {@link module:mixing mixing} calls.
*
* @param {Object | undefined} [settings]
* Default settings that should be used for subsequent {@link module:mixing mixing} calls.
* Initial default values will be used for settings that are not specified in the passed object.
* Pass <code>undefined</code>, <code>null</code>, non-object or to call without parameter
* to reset default settings to initial values.
* @alias module:mixing.setSettings
*/
mixing.setSettings = function(settings) {
defaultSettings = typeof settings === "object" ? settings : null;
};
return mixing;
module.exports = mixing;
return mixing;
}));

@@ -1,1 +0,1 @@

!function(a,b){if("object"==typeof exports)module.exports=b(require,exports,module);else if("function"==typeof define&&define.amd)define(["require","exports","module"],b);else{var c=function(b){return a[b]},d=a,e={exports:d};a.mixing=b(c,d,e)}}(this,function(a,b,c){function d(a,b){var c,d,e,f;if(Array.isArray(a)){if(a.length>0){c={},d=a.length;do c[a[--d]]=b||null;while(d)}}else f=typeof a,"string"===f||"symbol"===f?(c={},c[a]=b||null):"object"===f&&(a instanceof RegExp?e=a:c=a);return{map:c,regexp:e}}function e(a,b,c,d){var e,g,h,i,j,k=b[c],l=c.toString();d.ownProperty&&!b.hasOwnProperty(c)||d.copyMap&&!(c in d.copyMap)||d.copyRegExp&&!d.copyRegExp.test(l)||d.exceptions&&d.exceptions[c]||d.exceptRegExp&&d.exceptRegExp.test(l)||d.filter&&!d.filter.call(null,{field:c,value:k,target:a,source:b})||d.filterRegExp&&!d.filterRegExp.test("symbol"==typeof k?k.toString():k)||(h=d.otherNameMap,h&&c in h&&(c=h[c]),i=typeof k,!(d.recursive&&k&&"object"===i&&(j=a[c])&&"object"==typeof j)||Array.isArray(k)&&!d.mixFromArray||Array.isArray(j)&&!d.mixToArray?(e="function"===i,!d.overwrite&&c in a||e&&!d.copyFunc||(d.changeFunc?k=d.changeFunc.call(null,{field:c,value:k,target:a,source:b}):(g=d.change)&&c in g&&(k=g[c]),e&&d.funcToProto?a.constructor.prototype[c]=k:a[c]=k)):f(j,k,d.mixFromArray?f({oneSource:!0},d):d))}function f(a,b,c){var h=typeof a,i=typeof b;if(a&&("object"===h||"function"===h)&&b&&("object"===i||"function"===i)){var j;"object"!=typeof c||null===c?c=g||{}:g&&(j=g,g=null,c=f({},[c,j]),g=j),(!Array.isArray(b)||c.oneSource)&&(b=[b]);var k,l,m,n,o,p=Object.getOwnPropertySymbols,q=Object.getPrototypeOf,r={copyFunc:"copyFunc"in c?c.copyFunc:!0,funcToProto:Boolean(c.funcToProto),processSymbol:("processSymbol"in c?c.processSymbol:!0)&&"function"==typeof p,mixFromArray:Boolean(c.mixFromArray),mixToArray:Boolean(c.mixToArray),overwrite:Boolean(c.overwrite),ownProperty:Boolean(c.ownProperty),recursive:Boolean(c.recursive),otherNameMap:"otherName"in c?c.otherName:null},s=r.ownProperty,t=r.processSymbol,u=c.change,v=c.copy,w=c.except,x=c.filter;for(v&&(v=d(v),r.copyMap=v.map,r.copyRegExp=v.regexp),w&&(w=d(w,!0),r.exceptions=w.map,r.exceptRegExp=w.regexp),x&&(r["object"==typeof x?"filterRegExp":"filter"]=x),u&&(r["function"==typeof u?"changeFunc":"change"]=u),k=0,m=b.length;m>k;k++)if(j=b[k]){for(o in j)e(a,j,o,r);if(t){w={};do{for(v=p(j),l=0,n=v.length;n>l;l++)o=v[l],o in w||(e(a,j,o,r),w[o]=!0);j=s?null:q(j)}while(j)}}}return a}var g;return Array.isArray||(Array.isArray=function(a){return"[object Array]"===Object.prototype.toString.call(a)}),f.change=function(a,b){return f(a,a,{change:b,overwrite:!0,oneSource:!0})},f.copy=function(a,b){return f({},a,b)},f.mixToItems=function(a,b,c){for(var d=0,e=a.length;e>d;d++)a[d]=f(a[d],b,c);return a},f.clone=function(a){return f({},this,a)},f.filter=function(a){return f({},this,"function"==typeof a?{filter:a}:a)},f.map=function(a){return f({},this,"function"==typeof a?{change:a}:a)},f.mix=function(a,b){return f(this,a,b)},f.update=function(a){return f.change(this,a)},f.getSettings=function(){return g},f.setSettings=function(a){g="object"==typeof a?a:null},c.exports=f,f});
!function(o,e){if(void 0===o&&void 0!==window&&(o=window),"object"==typeof module&&module.exports)module.exports=e(require,exports,module);else if("function"==typeof define&&define.amd)define(["require","exports","module"],e);else{var r=o,t={exports:r};o.mixing=e(function(e){return o[e]},r,t)}}(this,function(e,o,r){var v;function d(e,o){var r,t,n,i;if(Array.isArray(e)){if(0<e.length)for(r={},t=e.length;r[e[--t]]=o||null,t;);}else"string"===(i=typeof e)||"symbol"===i?(r={})[e]=o||null:"object"===i&&(e instanceof RegExp?n=e:r=e);return{map:r,regexp:n}}function w(e,o,r,t){var n,i,c,p,u,f=o[r],a=r.toString();t.ownProperty&&!o.hasOwnProperty(r)||t.copyMap&&!(r in t.copyMap)||t.copyRegExp&&!t.copyRegExp.test(a)||t.exceptions&&t.exceptions[r]||t.exceptRegExp&&t.exceptRegExp.test(a)||t.filter&&!t.filter.call(null,{field:r,value:f,target:e,source:o})||t.filterRegExp&&!t.filterRegExp.test("symbol"==typeof f?f.toString():f)||((c=t.otherNameMap)&&r in c&&(r=c[r]),p=typeof f,!(t.recursive&&f&&"object"===p&&(u=e[r])&&"object"==typeof u)||Array.isArray(f)&&!t.mixFromArray||Array.isArray(u)&&!t.mixToArray?(n="function"===p,!t.overwrite&&r in e||n&&!t.copyFunc||(t.changeFunc?f=t.changeFunc.call(null,{field:r,value:f,target:e,source:o}):(i=t.change)&&r in i&&(f=i[r]),n&&t.funcToProto?e.constructor.prototype[r]=f:e[r]=f)):j(u,f,t.mixFromArray?j({oneSource:!0},t):t))}function j(e,o,r){var t=typeof e,n=typeof o;if(e&&("object"===t||"function"===t)&&o&&("object"===n||"function"===n)){var i;"object"!=typeof r||null===r?r=v||{}:v&&(i=v,v=null,r=j({},[r,i]),v=i),Array.isArray(o)&&!r.oneSource||(o=[o]);var c,p,u,f,a,l=Object.getOwnPropertySymbols,y=Object.getPrototypeOf,s={copyFunc:!("copyFunc"in r)||r.copyFunc,funcToProto:Boolean(r.funcToProto),processSymbol:(!("processSymbol"in r)||r.processSymbol)&&"function"==typeof l,mixFromArray:Boolean(r.mixFromArray),mixToArray:Boolean(r.mixToArray),overwrite:Boolean(r.overwrite),ownProperty:Boolean(r.ownProperty),recursive:Boolean(r.recursive),otherNameMap:"otherName"in r?r.otherName:null},g=s.ownProperty,m=s.processSymbol,x=r.change,h=r.copy,A=r.except,b=r.filter;for(h&&(h=d(h),s.copyMap=h.map,s.copyRegExp=h.regexp),A&&(A=d(A,!0),s.exceptions=A.map,s.exceptRegExp=A.regexp),b&&(s["object"==typeof b?"filterRegExp":"filter"]=b),x&&(s["function"==typeof x?"changeFunc":"change"]=x),c=0,u=o.length;c<u;c++)if(i=o[c]){for(a in i)w(e,i,a,s);if(m){A={};do{for(p=0,f=(h=l(i)).length;p<f;p++)(a=h[p])in A||(w(e,i,a,s),A[a]=!0);i=g?null:y(i)}while(i)}}}return e}return Array.isArray||(Array.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)}),j.assign=function(e){return j(e,Array.prototype.slice.call(arguments,1),{overwrite:!0,ownProperty:!0})},j.change=function(e,o){return j(e,e,{change:o,overwrite:!0,oneSource:!0})},j.copy=function(e,o){return j({},e,o)},j.mixToItems=function(e,o,r){for(var t=0,n=e.length;t<n;t++)e[t]=j(e[t],o,r);return e},j.clone=function(e){return j({},this,e)},j.filter=function(e){return j({},this,"function"==typeof e?{filter:e}:e)},j.map=function(e){return j({},this,"function"==typeof e?{change:e}:e)},j.mix=function(e,o){return j(this,e,o)},j.update=function(e){return j.change(this,e)},j.getSettings=function(){return v},j.setSettings=function(e){v="object"==typeof e?e:null},r.exports=j});

@@ -62,4 +62,3 @@ module.exports = function(grunt) {

objectToExport: "<%= name %>",
globalAlias: "<%= name %>",
indent: " "
globalAlias: "<%= name %>"
}

@@ -66,0 +65,0 @@ },

@@ -0,1 +1,6 @@

### 1.2.3 / 2018-12-02
* add `assign` method
* add types declaration file
### 1.2.2 / 2018-05-09

@@ -2,0 +7,0 @@

@@ -55,4 +55,6 @@ /**

&& (! settings.filter || settings.filter.call(null, {field: propName, value: propValue, target: destination, source: source}))
/* jshint -W122 */
&& (! settings.filterRegExp || settings.filterRegExp.test(typeof propValue === "symbol" ? propValue.toString() : propValue))) {
otherNameMap = settings.otherNameMap;
/* jshint +W122 */
otherNameMap = settings.otherNameMap;
if (otherNameMap && (propName in otherNameMap)) {

@@ -371,2 +373,23 @@ propName = otherNameMap[propName];

/**
* Copy values of all of the own properties from one or more source objects to the target object
* (similar to <code>Object.assign</code>).
* <br>
* This function is a "wrap" for the following code:
* <code><pre>
* mixing(destination, Array.prototype.slice.call(arguments, 1), {overwrite: true, ownProperty: true});
* </pre></code>
*
* @param {Object | Function} destination
* The target object into which fields and functions will be copied.
* @param {...Object} source
* An object whose contents will be copied.
* If a source is a falsy value (e.g. <code>null</code> or <code>undefined</code>), the source will be skipped.
* @return {Object}
* Modified <code>target</code> object.
*/
mixing.assign = function(destination) {
return mixing(destination, Array.prototype.slice.call(arguments, 1), {overwrite: true, ownProperty: true});
};
/**
* Change values of fields of given object.

@@ -373,0 +396,0 @@ * <br>

{
"name": "mixing",
"version": "1.2.2",
"version": "1.2.3",
"description": "Functions to mix, filter, change and copy/clone objects",
"main": "index.js",
"types": "./index.d.ts",
"directories": {

@@ -43,10 +44,10 @@ "doc": "doc",

"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-uglify": "~0.2.4",
"grunt-jsdoc": "^2.1.0",
"grunt-mocha-cli": "~1.1.0",
"grunt-push-release": "~0.1.9",
"grunt-umd": "~1.7.0",
"ink-docstrap": "^1.3.0"
"grunt": "1.0.3",
"grunt-contrib-jshint": "2.0.0",
"grunt-contrib-uglify": "4.0.0",
"grunt-jsdoc": "2.3.0",
"grunt-mocha-cli": "4.0.0",
"grunt-push-release": "0.1.9",
"grunt-umd": "3.0.0",
"ink-docstrap": "1.3.2"
},

@@ -53,0 +54,0 @@ "jspm": {

@@ -94,2 +94,3 @@ # mixing <a name="start"></a>

mixing({x: 5}, {a: 1, a2: "2man", a3: "a3", b: 4, copy5: 5, delta: "plus", e: 4}, {copy: /a/, filter: /^\D/}); // Returns {x: 5, a3: "a3", delta: "plus"}
mixing.assign({a: "start"}, {a: 1, b: 0}, {b: 2, c: 3, d: 4}, null, {e: "end"}); // Returns {a: 1, b: 2, c: 3, d: 4, e: "end"}

@@ -187,2 +188,7 @@ // Change default settings

### .assign(destination: Object, ...source: Object);
Copy values of all of the own properties from one or more source objects to the target object
(similar to `Object.assign`).
### .change(source: Array | Object, change: Function | Object);

@@ -189,0 +195,0 @@

@@ -28,2 +28,6 @@ // Tests for mixing component/module

function Core() {
}
Core.prototype = obj;
couple.a = sEmpty;

@@ -498,6 +502,2 @@ couple.g = emptyObj;

describe("mixing(destination, source, {ownProperty: ...}])", function() {
function Core() {
}
Core.prototype = obj;
describe("mixing(destination, source, {ownProperty: true}])", function() {

@@ -969,2 +969,26 @@ var settings = {ownProperty: true};

describe(".assign(destination, source1, source2, ...)", function() {
var assign = mixin.assign;
it("should copy own properties of source objects into destination", function() {
var src1 = new Core(),
src2 = {a: 2, b: 4, src2: true},
src3 = {a: 3, d: 10, src3: false, x: 5};
src1.e = 'e';
src1.src1 = 0;
expect( assign({}, src1) )
.eql( {e: 'e', src1: 0} );
expect( assign({a: 0, z: false}, src1, null, src2, undef, src3) )
.eql( {a: 3, z: false, e: 'e', src1: 0, b: 4, src2: true, d: 10, src3: false, x: 5} );
});
it("should return unmodified destination", function() {
expect( assign({test: true, data: 3}, 0, false, '', null, undef) )
.eql( {test: true, data: 3} );
expect( assign({a: 'start', b: 'end'}, new Core(), {}) )
.eql( {a: 'start', b: 'end'} );
});
});
describe(".change(source, change)", function() {

@@ -971,0 +995,0 @@ var change = mixin.change;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc