项目作者: gamtiq

项目描述 :
Functions to mix, filter, change and copy/clone objects
高级语言: JavaScript
项目地址: git://github.com/gamtiq/mixing.git
创建时间: 2013-07-01T17:07:21Z
项目社区:https://github.com/gamtiq/mixing

开源协议:

下载


" class="reference-link">mixing

Functions to mix, filter, change and copy/clone objects.
Supports processing of symbol property keys that are introduced in ECMAScript 2015.

mixing is like an improved version of Object.assign and is compatible with ECMAScript 3+.

Features

  • Overwrite all or only some fields, or do not change existent fields (specified by overwrite setting).
  • Mix recursively objects and arrays (use recursive and mixArray settings).
  • Copy only own or all fields of a source object (ownProperty setting).
  • Selectively copy fields from a source object (copy, except and filter settings).
  • Rename fields of a source object that are added into the target object (otherName setting).
  • Change values that are copied into the target object (change setting).
  • Several helpful “shortcut” functions that can be used standalone or as methods.
  1. const obj = {a: 1, b: {c: "", d: false}, e: [{f1: 1, f2: 2}, {f1: 0, f3: 9}], z: true};
  2. ...
  3. mixing(
  4. obj,
  5. {a: 3, b: {c: 1, c2: "abc"}, e: [{f2: -3}, {f1: 7, f2: null}, {f1: 10}], x: "way"},
  6. {overwrite: true, recursive: true, mixArray: true}
  7. );
  8. // 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}

NPM version
Build Status
Built with Grunt

Installation

Node

  1. npm install mixing

Ringo

  1. ringo-admin install gamtiq/mixing

Bower

  1. bower install mixing

AMD, script tag

Use dist/mixing.js or dist/mixing.min.js (minified version).

Usage

Node, Ringo

  1. var mixing = require("mixing");
  2. ...

AMD

  1. define(["path/to/dist/mixing.js"], function(mixing) {
  2. ...
  3. });

Bower, script tag

  1. <!-- Use bower_components/mixing/dist/mixing.js if the library was installed via Bower -->
  2. <script type="text/javascript" src="path/to/dist/mixing.js"></script>
  3. <script type="text/javascript">
  4. // mixing is available via mixing field of window object
  5. ...
  6. </script>

Examples

  1. var source = {a: 1, b: 2};
  2. var copy = mixing.copy(source); // Make a shallow copy of source
  3. var result = mixing({a: 1, b: 2}, {c: 3, d: 4}); // result is {a: 1, b: 2, c: 3, d: 4}
  4. mixing({a: 1, b: 2}, {a: "a", b: {}, c: 3, d: 4}); // Returns {a: 1, b: 2, c: 3, d: 4}
  5. mixing(
  6. {a: 1, b: 2, z: 100},
  7. {a: "a", b: {}, c: 3, d: 4},
  8. {overwrite: true}
  9. ); // Returns {a: "a", b: {}, c: 3, d: 4, z: 100}
  10. // Overwrite only fields whose names are matching regular expression
  11. mixing(
  12. {a: 1, b: 2, alfa: "omega", delta: "gamma", z: 100},
  13. {a: "a", b: {c: true}, d: 4, delta: 3, z: false},
  14. {overwrite: /^[a-c]$/}
  15. ); // Returns {a: "a", alfa: "omega", b: {c: true}, d: 4, delta: "gamma", z: 100}
  16. // Recursive mix
  17. mixing(
  18. {a: 1, b: {c: "", d: false}, e: [{f1: 1, f2: 2}, {f1: 0, f3: 9}], z: true},
  19. {a: 3, b: {c: 1, c2: "abc"}, e: [{f2: -3}, {f1: 7, f2: null}, {f1: 10}], x: "way"},
  20. {overwrite: true, recursive: true, mixArray: true}
  21. ); // 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}
  22. mixing(
  23. {a: 1, b: 2, c: "", d: false},
  24. {a: -1, b: null, c: true, d: "empty"},
  25. {overwrite: true, except: {a: false, b: true, c: null, d: "yes"}}
  26. ); // Returns {a: -1, b: 2, c: true, d: false}
  27. mixing(
  28. {a: 1, b: 2},
  29. {a3: 3, b: null, c4: "e5", d_97: new Date(), c: 3, "e-2": "empty"},
  30. {except: /\d/}
  31. ); // Returns {a: 1, b: 2, c: 3}
  32. mixing.copy(
  33. {a: 1, a2: 2, a3: "a3", b: 4, copy5: 5, d: "delta", e: "-123"},
  34. {except: /a/, filter: /\W/}
  35. ); // Returns {e: "-123"}
  36. mixing(
  37. {x: 5},
  38. {a: 1, a2: "2man", a3: "a3", b: 4, copy5: 5, delta: "plus", e: 4},
  39. {copy: /a/, filter: /^\D/}
  40. ); // Returns {x: 5, a3: "a3", delta: "plus"}
  41. mixing.assign(
  42. {a: "start"},
  43. {a: 1, b: 0},
  44. {b: 2, c: 3, d: 4},
  45. null,
  46. {e: "end"}
  47. ); // Returns {a: 1, b: 2, c: 3, d: 4, e: "end"}
  48. // Change default settings
  49. mixing.setSettings({overwrite: true, oneSource: true});
  50. // Mix arrays
  51. mixing([1, 2, 3], ["a", "b", "c", "d"]); // Returns ["a", "b", "c", "d"]
  52. mixing([3, 2, 1, 4, 5], [1, 2, 3]); // Returns [1, 2, 3, 4, 5]
  53. // Get redefined default settings
  54. mixing.getSettings(); // Returns {overwrite: true, oneSource: true}
  55. // Reset default settings to initial values
  56. mixing.setSettings();
  57. // Filter and change field values
  58. mixing({},
  59. [{a: 1, b: 100}, null, {c: 3, d: new Date(), e: 4}, {f: "str", g: 50}, undefined, {h: 7}],
  60. {
  61. except: ["a", "g"],
  62. filter: function(data) {
  63. var value = data.value;
  64. return typeof value === "number" && value < 10;
  65. },
  66. change: function(data) {
  67. var value = data.value;
  68. return value > 5 ? value * value : value;
  69. }
  70. }); // Returns {c: 3, e: 4, h: 49}
  71. mixing.change(
  72. {a: 1, b: "abc", c: null, d: 4444, e: false},
  73. {b: 22, c: 333, e: 55555}
  74. ); // Returns {a: 1, b: 22, c: 333, d:4444, e: 55555}
  75. // Change items in array
  76. mixing.mixToItems(
  77. [{a: 1, b: 2}, {b: 3}, 83, {}],
  78. {a: null, c: 9}
  79. ); // Returns [{a: 1, b: 2, c: 9}, {a: null, b: 3, c: 9}, 83, {a: null, c: 9}]
  80. mixing.mixToItems(
  81. [null, {a: 1, b: 2}, {b: 3, z: 0}, {}],
  82. {a: null, b: false},
  83. {overwrite: true}
  84. ); // Returns [null, {a: null, b: false}, {a: null, b: false, z: 0}, {a: null, b: false}]
  85. // Clone, filter, map, update
  86. var obj = {
  87. a: 1,
  88. b: 2,
  89. clone: mixing.clone,
  90. filter: mixing.filter,
  91. map: mixing.map,
  92. update: mixing.update
  93. };
  94. obj[Symbol("field")] = Symbol("value");
  95. var obj2 = obj.clone(); // obj2 is a shallow copy of obj (contains symbol property key)
  96. function isNumericValue(data) {
  97. return typeof data.value === "number";
  98. }
  99. var obj3 = obj.filter(isNumericValue); // {a: 1, b: 2}
  100. var obj4 = obj.map({
  101. filter: isNumericValue,
  102. change: function(data) {
  103. return data.value + data.value;
  104. }
  105. }); // {a: 2, b: 4}
  106. obj.update(function(data) {
  107. var value = data.value;
  108. return typeof value === "number"
  109. ? ++value
  110. : value;
  111. }); // obj is {a: 2, b: 3, clone: ...}

See additional examples in tests.

API

See docs for details.

mixing(destination: Object, source: Array | Object, [settings: Object]);

Copy/add all fields and functions from source object(s) into the destination object.
As a result the destination object may be modified.

Several settings are supported (see doc/module-mixing.html for details):

  • copyFunc: Boolean - Should functions be copied?
  • funcToProto: Boolean - Should functions be copied into prototype of the destination object’s constructor?
  • processSymbol: Boolean - Should symbol property keys be processed?
  • overwrite: Boolean | Function | RegExp - Specifies whether a field/function should be overwritten when it exists in the target object.
  • recursive: Boolean - Should this function be called recursively when field’s value of the destination and source object is an object?
  • mixFromArray: Boolean - Should in recursive mode contents of a field of the source object be copied when the field’s value is an array?
  • 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?
  • mixArray: Boolean - Default value for mixFromArray and mixToArray settings.
  • oneSource: Boolean - Should source array be interpreted directly as copied object instead of list of source objects?
  • ownProperty: Boolean - Should only own properties of the source object be copied into the target object?
  • copy: Array | Object | RegExp | String | Symbol - Array, object, regular expression or string/symbol that defines names of fields/functions that should be copied.
  • except: Array | Object | RegExp | String | Symbol - Array, object, regular expression or string/symbol that defines names of fields/functions that shouldn’t be copied.
  • filter: Function | RegExp - Function or regular expression that can be used to select elements that should be copied.
  • otherName: Object - Defines “renaming table” for copied elements.
  • change: Function | Object - Function or object that gives ability to change values that should be copied.

.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);

Change values of fields of given object.

.copy(source: Array | Object, [settings: Object]);

Make a copy of source object(s).

.mixToItems(destinationList: Array, source: Array | Object, [settings: Object]);

Copy fields from source object(s) into every object item of given array.

.clone([settings: Object]);

Make a copy of this object.
This function can be transferred to an object to use as a method.
For example:

  1. SomeClass.prototype.clone = mixing.clone;
  2. ...
  3. var obj = new SomeClass();
  4. ...
  5. var copy = obj.clone();

.filter(filter: Function | Object);

Filter this object.
This function can be transferred to an object to use as a method.
For example:

  1. SomeClass.prototype.filter = mixing.filter;
  2. ...
  3. var obj = new SomeClass();
  4. ...
  5. var result = obj.filter(function(data) {
  6. // data.source is obj, data.target is result
  7. ...
  8. });

.map(change: Function | Object);

Copy and change values of fields of this object.
This function can be transferred to an object to use as a method.
For example:

  1. SomeClass.prototype.map = mixing.map;
  2. ...
  3. var obj = new SomeClass();
  4. ...
  5. var result = obj.map(function(data) {
  6. // data.source is obj, data.target is result
  7. ...
  8. });

.mix(source: Array | Object, [settings: Object]);

Copy/add all fields and functions from source objects into this object.
As a result this object may be modified.
This function can be transferred to an object to use as a method.
For example:

  1. SomeClass.prototype.mix = mixing.mix;
  2. ...
  3. var obj = new SomeClass();
  4. ...
  5. obj.mix([obj1, obj2]);

.update(change: Function | Object);

Change values of fields of this object.
This function can be transferred to an object to use as a method.
For example:

  1. SomeClass.prototype.update = mixing.update;
  2. ...
  3. var obj = new SomeClass();
  4. ...
  5. obj.update({a: 2, b: ""});

.getSettings();

Return default settings that were set earlier.

.setSettings([settings: Object]);

Set (redefine, reset) default settings that should be used for subsequent mixing calls.

Licence

MIT