Skip to content

Commit

Permalink
feat: re-implemented basic feature as a set operation
Browse files Browse the repository at this point in the history
BREAKING CHANGE: export structure updated
  • Loading branch information
andres-kovalev committed Oct 19, 2019
1 parent 4761789 commit 315df41
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 59 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./src/update');
module.exports = require('./src');
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const set = require('./set');

module.exports = {
set
};
56 changes: 56 additions & 0 deletions src/set/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module.exports = set;

function set(object, path, value) {
const pathArray = Array.isArray(path)
? path
: path.split('.');

return doSet(object, pathArray, 0, value);
}

function doSet(object, path, index, value) {
if (index === path.length) {
return value;
}

const key = path[index];

if (!key) {
return doSet(object, path, index + 1, value);
}

const source = object || createSource(key);
const item = source[key];
const newItem = doSet(item, path, index + 1, value);

if (item === newItem) {
return source;
}

if (!Array.isArray(source)) {
return Object.assign({}, source, {
[key]: newItem
});
}

if (isNumber(key)) {
return source
.slice(0, key)
.concat(
[ newItem ],
source.slice(key + 1)
);
}

return Object.assign(source.slice(), {
[key]: newItem
});
}

function createSource(key) {
return isNumber(key) ? [] : {};
}

function isNumber(value) {
return value.toString() === parseInt(value, 0).toString();
}
50 changes: 0 additions & 50 deletions src/update.js

This file was deleted.

16 changes: 8 additions & 8 deletions tests/update.spec.js → tests/set.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const update = require('../src/update');
const set = require('../src/set');

describe('update', () => {
describe('set', () => {
const source = {
a: {
a1: 1,
Expand All @@ -17,15 +17,15 @@ describe('update', () => {
it('should update value', () => {
const newValue = 6;

const result = update(source, [ 'b', 'b1' ], newValue);
const result = set(source, [ 'b', 'b1' ], newValue);

expect(result.b.b1).to.be.equal(newValue);
});

it('should update value not considering dots as delimiter', () => {
const newValue = 6;

const result = update(source, [ 'b.b1' ], newValue);
const result = set(source, [ 'b.b1' ], newValue);

expect(result['b.b1']).to.be.equal(newValue);
});
Expand All @@ -36,7 +36,7 @@ describe('update', () => {

let result;
beforeEach(() => {
result = update(source, 'b.b1', newValue);
result = set(source, 'b.b1', newValue);
});

it('should update value considering dots as delimiter', () => {
Expand All @@ -62,23 +62,23 @@ describe('update', () => {
it('should ignore empty path items', () => {
const newValue = 6;

const result = update(source, 'b..b1', newValue);
const result = set(source, 'b..b1', newValue);

expect(result.b.b1).to.be.equal(newValue);
});

it('should create intermediate items if not exists', () => {
const newValue = 6;

const result = update(source, 'c.c1', newValue);
const result = set(source, 'c.c1', newValue);

expect(result.c.c1).to.be.equal(newValue);
});

it('should create arrays item if not exists and addressed by index', () => {
const newValue = 6;

const result = update(source, 'c.0', newValue);
const result = set(source, 'c.0', newValue);

expect(result.c).to.be.an('array');
expect(result.c[0]).to.be.equal(newValue);
Expand Down

0 comments on commit 315df41

Please sign in to comment.