main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
870 bytes
|
Line | |
---|
1 | import _isArray from "./_isArray.js";
|
---|
2 | import _isInteger from "./_isInteger.js";
|
---|
3 | /**
|
---|
4 | * Makes a shallow clone of an object, setting or overriding 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 {*} val The new value
|
---|
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 _assoc(prop, val, obj) {
|
---|
17 | if (_isInteger(prop) && _isArray(obj)) {
|
---|
18 | var arr = [].concat(obj);
|
---|
19 | arr[prop] = val;
|
---|
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] = val;
|
---|
30 | return result;
|
---|
31 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.