{"id":19009192,"url":"https://github.com/gamtiq/mixing","last_synced_at":"2025-04-22T22:46:18.322Z","repository":{"id":9271981,"uuid":"11101531","full_name":"gamtiq/mixing","owner":"gamtiq","description":"Functions to mix, filter, change and copy/clone objects","archived":false,"fork":false,"pushed_at":"2021-05-09T14:52:38.000Z","size":1101,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-11T22:38:51.281Z","etag":null,"topics":["assign","change","clone","copy","filter","map","merge","mix","mixin","modify","object","update"],"latest_commit_sha":null,"homepage":"https://gamtiq.github.io/mixing/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gamtiq.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-07-01T17:07:21.000Z","updated_at":"2023-07-20T03:24:46.000Z","dependencies_parsed_at":"2022-09-01T05:23:07.938Z","dependency_job_id":null,"html_url":"https://github.com/gamtiq/mixing","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fmixing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fmixing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fmixing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gamtiq%2Fmixing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gamtiq","download_url":"https://codeload.github.com/gamtiq/mixing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250337913,"owners_count":21414102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assign","change","clone","copy","filter","map","merge","mix","mixin","modify","object","update"],"created_at":"2024-11-08T19:07:04.398Z","updated_at":"2025-04-22T22:46:18.281Z","avatar_url":"https://github.com/gamtiq.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mixing \u003ca name=\"start\"\u003e\u003c/a\u003e\n\nFunctions to mix, filter, change and copy/clone objects.\nSupports processing of symbol property keys that are introduced in ECMAScript 2015.\n\n`mixing` is like an improved version of `Object.assign` and is compatible with ECMAScript 3+.\n\n### Features\n\n* Overwrite all or only some fields, or do not change existent fields (specified by `overwrite` setting).\n* Mix recursively objects and arrays (use `recursive` and `mixArray` settings).\n* Copy only own or all fields of a source object (`ownProperty` setting).\n* Selectively copy fields from a source object (`copy`, `except` and `filter` settings).\n* Rename fields of a source object that are added into the target object (`otherName` setting).\n* Change values that are copied into the target object (`change` setting).\n* Several helpful \"shortcut\" functions that can be used standalone or as methods.\n\n```js\nconst obj = {a: 1, b: {c: \"\", d: false}, e: [{f1: 1, f2: 2}, {f1: 0, f3: 9}], z: true};\n...\nmixing(\n    obj,\n    {a: 3, b: {c: 1, c2: \"abc\"}, e: [{f2: -3}, {f1: 7, f2: null}, {f1: 10}], x: \"way\"},\n    {overwrite: true, recursive: true, mixArray: true}\n);\n// obj is {a: 3, b: {c: 1, d: false, c2: \"abc\"}, e: [{f1: 1, f2: -3}, {f1: 7, f3: 9, f2: null}, {f1: 10}], x: \"way\", z: true}\n```\n\n[![NPM version](https://badge.fury.io/js/mixing.png)](http://badge.fury.io/js/mixing)\n[![Build Status](https://travis-ci.org/gamtiq/mixing.png)](https://travis-ci.org/gamtiq/mixing)\n[![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)\n\n* [Usage](#usage)\n* [Examples](#examples)\n* [API](#api)\n* [Related projects](#related)\n\n## Installation\n\n### Node\n\n    npm install mixing\n\n### [Ringo](https://ringojs.org)\n\n    ringo-admin install gamtiq/mixing\n\n### [Bower](http://bower.io)\n\n    bower install mixing\n\n### AMD, script tag\n\nUse `dist/mixing.js` or `dist/mixing.min.js` (minified version).\n\n## Usage \u003ca name=\"usage\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n### Node, Ringo\n\n```js\nvar mixing = require(\"mixing\");\n...\n```\n\n### AMD\n\n```js\ndefine([\"path/to/dist/mixing.js\"], function(mixing) {\n    ...\n});\n```\n\n### Bower, script tag\n\n```html\n\u003c!-- Use bower_components/mixing/dist/mixing.js if the library was installed via Bower --\u003e\n\u003cscript type=\"text/javascript\" src=\"path/to/dist/mixing.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\n    // mixing is available via mixing field of window object\n    ...\n\u003c/script\u003e\n```\n\n### Examples \u003ca name=\"examples\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n```js\nvar source = {a: 1, b: 2};\nvar copy = mixing.copy(source);   // Make a shallow copy of source\nvar result = mixing({a: 1, b: 2}, {c: 3, d: 4});   // result is {a: 1, b: 2, c: 3, d: 4}\nmixing({a: 1, b: 2}, {a: \"a\", b: {}, c: 3, d: 4});   // Returns {a: 1, b: 2, c: 3, d: 4}\nmixing(\n    {a: 1, b: 2, z: 100},\n    {a: \"a\", b: {}, c: 3, d: 4},\n    {overwrite: true}\n);   // Returns {a: \"a\", b: {}, c: 3, d: 4, z: 100}\n// Overwrite only fields whose names are matching regular expression\nmixing(\n    {a: 1, b: 2, alfa: \"omega\", delta: \"gamma\", z: 100},\n    {a: \"a\", b: {c: true}, d: 4, delta: 3, z: false},\n    {overwrite: /^[a-c]$/}\n);   // Returns {a: \"a\", alfa: \"omega\", b: {c: true}, d: 4, delta: \"gamma\", z: 100}\n// Recursive mix\nmixing(\n    {a: 1, b: {c: \"\", d: false}, e: [{f1: 1, f2: 2}, {f1: 0, f3: 9}], z: true},\n    {a: 3, b: {c: 1, c2: \"abc\"}, e: [{f2: -3}, {f1: 7, f2: null}, {f1: 10}], x: \"way\"},\n    {overwrite: true, recursive: true, mixArray: true}\n);   // Returns {a: 3, b: {c: 1, d: false, c2: \"abc\"}, e: [{f1: 1, f2: -3}, {f1: 7, f3: 9, f2: null}, {f1: 10}], x: \"way\", z: true}\n\nmixing(\n    {a: 1, b: 2, c: \"\", d: false},\n    {a: -1, b: null, c: true, d: \"empty\"},\n    {overwrite: true, except: {a: false, b: true, c: null, d: \"yes\"}}\n);   // Returns {a: -1, b: 2, c: true, d: false}\nmixing(\n    {a: 1, b: 2},\n    {a3: 3, b: null, c4: \"e5\", d_97: new Date(), c: 3, \"e-2\": \"empty\"},\n    {except: /\\d/}\n);   // Returns {a: 1, b: 2, c: 3}\nmixing.copy(\n    {a: 1, a2: 2, a3: \"a3\", b: 4, copy5: 5, d: \"delta\", e: \"-123\"},\n    {except: /a/, filter: /\\W/}\n);   // Returns {e: \"-123\"}\nmixing(\n    {x: 5},\n    {a: 1, a2: \"2man\", a3: \"a3\", b: 4, copy5: 5, delta: \"plus\", e: 4},\n    {copy: /a/, filter: /^\\D/}\n);   // Returns {x: 5, a3: \"a3\", delta: \"plus\"}\nmixing.assign(\n    {a: \"start\"},\n    {a: 1, b: 0},\n    {b: 2, c: 3, d: 4},\n    null,\n    {e: \"end\"}\n);   // Returns {a: 1, b: 2, c: 3, d: 4, e: \"end\"}\n\n// Change default settings\nmixing.setSettings({overwrite: true, oneSource: true});\n// Mix arrays\nmixing([1, 2, 3], [\"a\", \"b\", \"c\", \"d\"]);   // Returns [\"a\", \"b\", \"c\", \"d\"]\nmixing([3, 2, 1, 4, 5], [1, 2, 3]);   // Returns [1, 2, 3, 4, 5]\n// Get redefined default settings\nmixing.getSettings();   // Returns {overwrite: true, oneSource: true}\n// Reset default settings to initial values\nmixing.setSettings();\n\n// Filter and change field values\nmixing({}, \n       [{a: 1, b: 100}, null, {c: 3, d: new Date(), e: 4}, {f: \"str\", g: 50}, undefined, {h: 7}], \n       {\n           except: [\"a\", \"g\"],\n           filter: function(data) {\n               var value = data.value;\n               return typeof value === \"number\" \u0026\u0026 value \u003c 10;\n           },\n           change: function(data) {\n               var value = data.value;\n               return value \u003e 5 ? value * value : value;\n           }\n       });   // Returns {c: 3, e: 4, h: 49}\n\nmixing.change(\n    {a: 1, b: \"abc\", c: null, d: 4444, e: false},\n    {b: 22, c: 333, e: 55555}\n);   // Returns {a: 1, b: 22, c: 333, d:4444, e: 55555}\n\n// Change items in array\nmixing.mixToItems(\n    [{a: 1, b: 2}, {b: 3}, 83, {}],\n    {a: null, c: 9}\n);   // Returns [{a: 1, b: 2, c: 9}, {a: null, b: 3, c: 9}, 83, {a: null, c: 9}]\nmixing.mixToItems(\n    [null, {a: 1, b: 2}, {b: 3, z: 0}, {}],\n    {a: null, b: false},\n    {overwrite: true}\n);   // Returns [null, {a: null, b: false}, {a: null, b: false, z: 0}, {a: null, b: false}]\n\n// Clone, filter, map, update\nvar obj = {\n    a: 1,\n    b: 2,\n    clone: mixing.clone,\n    filter: mixing.filter,\n    map: mixing.map,\n    update: mixing.update\n};\nobj[Symbol(\"field\")] = Symbol(\"value\");\nvar obj2 = obj.clone();   // obj2 is a shallow copy of obj (contains symbol property key)\n\nfunction isNumericValue(data) {\n    return typeof data.value === \"number\";\n}\n\nvar obj3 = obj.filter(isNumericValue);   // {a: 1, b: 2}\n\nvar obj4 = obj.map({\n    filter: isNumericValue,\n    change: function(data) {\n        return data.value + data.value;\n    }\n});   // {a: 2, b: 4}\n\nobj.update(function(data) {\n    var value = data.value;\n    return typeof value === \"number\"\n                ? ++value\n                : value;\n});   // obj is {a: 2, b: 3, clone: ...}\n```\n\nSee additional examples in tests.\n\n## API \u003ca name=\"api\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\nSee [`docs`](https://gamtiq.github.io/mixing/) for details.\n\n### mixing(destination: Object, source: Array | Object, [settings: Object]);\n\nCopy/add all fields and functions from `source` object(s) into the `destination` object.\nAs a result the `destination` object may be modified.\n\nSeveral settings are supported (see `doc/module-mixing.html` for details):\n\n* `copyFunc`: `Boolean` - Should functions be copied?\n* `funcToProto`: `Boolean` - Should functions be copied into `prototype` of the `destination` object's `constructor`?\n* `processSymbol`: `Boolean` - Should symbol property keys be processed?\n* `overwrite`: `Boolean | Function | RegExp` - Specifies whether a field/function should be overwritten when it exists in the target object.\n* `recursive`: `Boolean` - Should this function be called recursively when field's value of the `destination` and `source` object is an object?\n* `mixFromArray`: `Boolean` - Should in recursive mode contents of a field of the source object be copied when the field's value is an array?\n* `mixToArray`: `Boolean` - Should in recursive mode 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?\n* `mixArray`: `Boolean` - Default value for `mixFromArray` and `mixToArray` settings.\n* `oneSource`: `Boolean` - Should `source` array be interpreted directly as copied object instead of list of source objects?\n* `ownProperty`: `Boolean` - Should only own properties of the source object be copied into the target object?\n* `copy`: `Array | Object | RegExp | String | Symbol` - Array, object, regular expression or string/symbol that defines names of fields/functions that should be copied.\n* `except`: `Array | Object | RegExp | String | Symbol` - Array, object, regular expression or string/symbol that defines names of fields/functions that shouldn't be copied.\n* `filter`: `Function | RegExp` - Function or regular expression that can be used to select elements that should be copied.\n* `otherName`: `Object` - Defines \"renaming table\" for copied elements.\n* `change`: `Function | Object` - Function or object that gives ability to change values that should be copied.\n\n### .assign(destination: Object, ...source: Object);\n\nCopy values of all of the own properties from one or more source objects to the target object\n(similar to `Object.assign`).\n\n### .change(source: Array | Object, change: Function | Object);\n\nChange values of fields of given object.\n\n### .copy(source: Array | Object, [settings: Object]);\n\nMake a copy of source object(s).\n\n### .mixToItems(destinationList: Array, source: Array | Object, [settings: Object]);\n\nCopy fields from source object(s) into every object item of given array.\n\n### .clone([settings: Object]);\n\nMake a copy of `this` object.\nThis function can be transferred to an object to use as a method.\nFor example:\n```js\nSomeClass.prototype.clone = mixing.clone;\n...\nvar obj = new SomeClass();\n...\nvar copy = obj.clone();\n```\n\n### .filter(filter: Function | Object);\n\nFilter `this` object.\nThis function can be transferred to an object to use as a method.\nFor example:\n```js\nSomeClass.prototype.filter = mixing.filter;\n...\nvar obj = new SomeClass();\n...\nvar result = obj.filter(function(data) {\n    // data.source is obj, data.target is result\n    ...\n});\n```\n\n### .map(change: Function | Object);\n\nCopy and change values of fields of `this` object.\nThis function can be transferred to an object to use as a method.\nFor example:\n```js\nSomeClass.prototype.map = mixing.map;\n...\nvar obj = new SomeClass();\n...\nvar result = obj.map(function(data) {\n    // data.source is obj, data.target is result\n    ...\n});\n```\n\n### .mix(source: Array | Object, [settings: Object]);\n\nCopy/add all fields and functions from source objects into `this` object.\nAs a result `this` object may be modified.\nThis function can be transferred to an object to use as a method.\nFor example:\n```js\nSomeClass.prototype.mix = mixing.mix;\n...\nvar obj = new SomeClass();\n...\nobj.mix([obj1, obj2]);\n```\n\n### .update(change: Function | Object);\n\nChange values of fields of `this` object.\nThis function can be transferred to an object to use as a method.\nFor example:\n```js\nSomeClass.prototype.update = mixing.update;\n...\nvar obj = new SomeClass();\n...\nobj.update({a: 2, b: \"\"});\n```\n\n### .getSettings();\n\nReturn default settings that were set earlier.\n\n### .setSettings([settings: Object]);\n\nSet (redefine, reset) default settings that should be used for subsequent `mixing` calls.\n\n## Related projects \u003ca name=\"related\"\u003e\u003c/a\u003e [\u0026#x2191;](#start)\n\n* [adam](https://github.com/gamtiq/adam)\n* [basespace](https://github.com/gamtiq/basespace)\n* [extend](https://github.com/gamtiq/extend)\n* [teo](https://github.com/gamtiq/teo)\n\n## Licence\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgamtiq%2Fmixing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgamtiq%2Fmixing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgamtiq%2Fmixing/lists"}