Skip to content

Commit

Permalink
Merge aaae116 into e0513de
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenPaulWright committed Mar 2, 2019
2 parents e0513de + aaae116 commit 372edd3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [0.2.7] - 2019-3-1
### Changed
- [set](docs/set.md) only adds new items once at the end instead of progressively

## [0.2.6] - 2019-2-17
### Changed
- Added ignoreKeys arg to [mapOwn](docs/mapOwn.md) and [clone](docs/clone.md)
Expand Down Expand Up @@ -47,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [diffUpdate](docs/diffUpdate.md)
- [intersection](docs/intersection.md)

[0.2.7]: https://github.com/DarrenPaulWright/object-agent/compare/v0.2.6...0.2.7
[0.2.6]: https://github.com/DarrenPaulWright/object-agent/compare/v0.2.5...0.2.6
[0.2.5]: https://github.com/DarrenPaulWright/object-agent/compare/v0.2.4...0.2.5
[0.2.4]: https://github.com/DarrenPaulWright/object-agent/compare/v0.2.3...0.2.4
Expand Down
2 changes: 1 addition & 1 deletion docs/set.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A javascript library for working with objects
<a name="set"></a>

## set(object, path, value)
Sets a nested value in an object.
Sets a nested value in an object. Keys in the path that don't exist at any point in the object will be created and added to the object once.

**Kind**: global function

Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "object-agent",
"version": "0.2.6",
"version": "0.2.7",
"description": "A javascript library for working with objects",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -68,6 +68,7 @@
"karma-mocha": "^1.3.0",
"karma-webpack": "^3.0.5",
"mocha": "^5.2.0",
"on-change": "^1.0.0",
"test-runner-config": "^0.5.0",
"wallaby-webpack": "^3.9.13",
"webpack": "^4.29.3",
Expand Down
22 changes: 18 additions & 4 deletions src/set.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isNumber } from 'type-enforcer';
import { isInteger } from 'type-enforcer';
import parsePath from './utility/parsePath';

/**
* Sets a nested value in an object.
* Sets a nested value in an object. Keys in the path that don't exist at any point in the object will be created and added to the object once.
*
* @example
* ``` javascript
Expand Down Expand Up @@ -36,14 +36,28 @@ import parsePath from './utility/parsePath';
export default (object, path, value) => {
path = parsePath(path);
const last = path.length - 1;
const buildNew = (index) => isInteger(path[index + 1], true) ? [] : {};
let baseItem;
let baseKey;
let baseValue;

path.forEach((key, index) => {
if (index === last) {
object[key] = value;
}
else if (!object[key]) {
object[key] = isNumber(path[index + 1], true) ? [] : {};
else if (!(key in object)) {
if (!baseItem) {
baseItem = object;
baseKey = key;
baseValue = object = buildNew(index);
return;
}
object[key] = buildNew(index);
}
object = object[key];
});

if (baseItem) {
baseItem[baseKey] = baseValue;
}
};
9 changes: 7 additions & 2 deletions tests/set.Test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from 'chai';
import onChange from 'on-change';
import { set } from '../src/';

describe('set', () => {
Expand Down Expand Up @@ -133,7 +134,10 @@ describe('set', () => {
});

it('should create objects and arrays then set the value', () => {
const object = {};
const object = onChange({}, () => {
testVar++;
});
let testVar = 0;
const compare = {
level1: [, , { // eslint-disable-line no-sparse-arrays
level2: {
Expand All @@ -142,9 +146,10 @@ describe('set', () => {
}]
};

set(object, ['level1', 2, 'level2', 'level3'], 'meh');
set(object, ['level1', '2', 'level2', 'level3'], 'meh');

assert.deepEqual(object, compare);
assert.equal(testVar, 1);
});

});

0 comments on commit 372edd3

Please sign in to comment.