Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/setMany #43

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ dotProp.set(obj, 'foo.$end', 'platin-unicorn')

```

### setMany

Set multiple nested values by passing a map of path: value

```javascript
// Setter
var obj = { foo: { bar: 'a' } };
// var obj2 = dotProp.set(dotProp.set(obj, 'foo.bar', 'b') , 'foo.baz', 'x');
var obj2 = dotProp.setMany(obj, { 'foo.bar': 'b', 'foo.baz': 'x' });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need a many group, I would expect: setMany, getMany and deleteMany.
In that case I would expect an array as input and output for getMany and not an object. Therefore I also expect an array as input for setMany.

//obj2 => {foo: {bar: 'b', baz: 'x'}}
```

### delete

Expand Down
13 changes: 8 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
type Path = number | string | (string | number)[];

interface DotPropImmutable {
get<I, T = any>(source: I, path: number | string | (string | number)[], defaultValue?: any): T;
set<I, T = any>(source: I, path: number | string | (string | number)[], value: any): T;
delete<I, T = any>(source: I, path: number | string | (string | number)[]): Partial<T>;
toggle<I, T = any>(source: I, path: number | string | (string | number)[]): T;
merge<I, T = any>(source: I, path: number | string | (string | number)[], value: any): T;
get<I, T = any>(source: I, path: Path, defaultValue?: any): T;
set<I, T = any>(source: I, path: Path, value: any): T;
setMany<I, T = any>(source, paths: Path[]): T;
delete<I, T = any>(source: I, path: Path): Partial<T>;
toggle<I, T = any>(source: I, path: Path): T;
merge<I, T = any>(source: I, path: Path, value: any): T;
}

declare const dotPropImmutable: DotPropImmutable;
Expand Down
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ function set(obj, prop, value) {
return setPropImmutableRec(obj, prop, value, 0);
}


/**
* Set many values defined in a map on the obj
* @param obj
* @param map
*/
function setMany(obj, map) {
return Object.keys(map).reduce((acc, path) => {
return set(acc, path, map[path]);
}, obj);
}

/**
* Get a value by a dot path.
* @param obj The object to evaluate.
Expand Down Expand Up @@ -169,5 +181,6 @@ module.exports = {
get,
delete: _delete,
toggle,
merge
merge,
setMany
};
15 changes: 15 additions & 0 deletions test/dot-prop-immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,21 @@ describe('dot-prop-immutable.spec.js', () => {
});
});

describe('when setMany', () => {

it('Sets a map of elements', () => {
expect(dotProp.setMany({ foo: 0, bar: { baz: 1 } }, { foo: 2, 'bar.baz': 3 })).to.eql(
{ foo: 2, bar: { baz: 3 } }
);
});

it('Sets a map of elements un existent', () => {
expect(dotProp.setMany({}, { foo: 2, 'bar.baz': 3 })).to.eql(
{ foo: 2, bar: { baz: 3 } }
);
});
});

describe('when get', () => {

describe('when have an object', () => {
Expand Down