main
Last change
on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
926 bytes
|
Rev | Line | |
---|
[d24f17c] | 1 | import _isArray from "./_isArray.js";
|
---|
| 2 | import _isInteger from "./_isInteger.js";
|
---|
| 3 | /**
|
---|
| 4 | * Makes a shallow clone of an object, applying the given fn to the specified
|
---|
| 5 | * property with the given value. Note that this copies and flattens prototype
|
---|
| 6 | * properties onto the new object as well. All non-primitive properties are
|
---|
| 7 | * copied by reference.
|
---|
| 8 | *
|
---|
| 9 | * @private
|
---|
| 10 | * @param {String|Number} prop The property name to set
|
---|
| 11 | * @param {Function} fn The function to apply to the property
|
---|
| 12 | * @param {Object|Array} obj The object to clone
|
---|
| 13 | * @return {Object|Array} A new object equivalent to the original except for the changed property.
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | export default function _modify(prop, fn, obj) {
|
---|
| 17 | if (_isInteger(prop) && _isArray(obj)) {
|
---|
| 18 | var arr = [].concat(obj);
|
---|
| 19 | arr[prop] = fn(arr[prop]);
|
---|
| 20 | return arr;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | var result = {};
|
---|
| 24 |
|
---|
| 25 | for (var p in obj) {
|
---|
| 26 | result[p] = obj[p];
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | result[prop] = fn(result[prop]);
|
---|
| 30 | return result;
|
---|
| 31 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.