Skip to content

Commit

Permalink
Set now accepts number as path, changed version to 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz-pluszczewski committed Jan 28, 2018
1 parent d3d8ee4 commit 15c6902
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

#### 1.4.2
- Updated docs

#### 1.5.0
- Set function now accepts number as path (interpreted as array index)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perfect-immutable",
"version": "1.4.2",
"version": "1.5.0",
"description": "Library to provide immutable methods (like immutable set similar to lodash's _.set) on standard JS objects",
"repository": {
"type": "git",
Expand Down
9 changes: 5 additions & 4 deletions src/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ const setValue = (target, field, value, setFunction = false) => {
/**
* Works like lodash _.set() but does not mutate target, works with arrays
* @param {object|array} target source object or array
* @param {string|object} path or index in object to set value in or object with paths/indexes as keys and values as values (if path is an object it ignores 'value')
* @param {string|object|number} argPath or index in object to set value in or object with paths/indexes as keys and values as values (if path is an object it ignores 'value')
* @param {any|function} value to set in given path/index or transform function that accepts current value and returns new one
* @param {boolean} setFunction if true and value provided is a function it will be treated as simple value and saved to the target (instead of used as transform)
* @return {object|array} new object or array with value(s) changed
*/
const immutableSet = (target, path, value = null, setFunction = false) => {
const immutableSet = (target, argPath, value = null, setFunction = false) => {
if (!isArray(target) && !isPlainObject(target)) {
throw new Error(`First argument provided to immutableSet function must be a plain object or array but it's type is ${typeof target}`);
}
if (!isArray(path) && !isPlainObject(path) && !isString(path)) {
throw new Error(`Path passed to immutableSet function must be a string, array of strings and numbers or plain object but it's type is ${typeof path}`);
if (!isArray(argPath) && !isPlainObject(argPath) && !isString(argPath) && !isNumber(argPath)) {
throw new Error(`Path passed to immutableSet function must be a string, number, array of strings and numbers or plain object but it's type is ${typeof argPath}`);
}
const path = isNumber(argPath) ? `[${argPath}]` : argPath;

if (!path || (isArray(path) && !path.length)) {
return value;
Expand Down
15 changes: 13 additions & 2 deletions test/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,18 @@ describe('set', () => {
it('should throw an error when provided with unsupported value as a first argument (not object/array)', () => {
expect(() => set(2, '[2]', 'value')).to.throw(Error);
});
it('should throw an error when provided with unsupported value as a second argument (not object/array/string)', () => {
expect(() => set({ foo: 'bar' }, 2, 'value')).to.throw(Error);
it('should not throw an error when provided with number as a second argument', () => {
expect(() => set(['foo', 'bar', 'baz'], 1, 'value')).to.not.throw(Error);
});
it('should set a value when provided with number as a second argument', () => {
const arr1 = ['foo', 'bar', 'baz'];

const arr2 = set(arr1, 1, 'newValue');

expect(arr2, 'Newly created object equals source object. It has probably been mutated').to.not.equal(arr1);
expect(arr2).to.deep.equal(['foo', 'newValue', 'baz']);
});
it('should throw an error when provided with unsupported value as a second argument (not object/array/string/number)', () => {
expect(() => set({ foo: 'bar' }, () => {}, 'value')).to.throw(Error); // eslint-disable-line no-empty-function
});
});

0 comments on commit 15c6902

Please sign in to comment.