This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Implement set operation for vanilla objects
- Loading branch information
Showing
4 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import createPolymorphFunction from '../../../util/createPolymorphFunction/index.js'; | ||
import resolveObjectPath from '../../../util/resolveObjectPath/index.js'; | ||
|
||
import $get from '../../../projections/atoms/get/index.js'; | ||
|
||
// | ||
// Helper function to peform the necessary recursion | ||
// | ||
const recursivelySetValueInObject = (object, value, path) => { | ||
if (path.length === 0) { | ||
return value; | ||
} | ||
|
||
object[path[0]] = recursivelySetValueInObject(object[path[0]], value, path.slice(1)); | ||
return object; | ||
}; | ||
|
||
// | ||
// Sets a value inside an object and returns the resulting object | ||
// | ||
export default createPolymorphFunction( | ||
path => value => subject => { | ||
if (!$get(path, subject)) { | ||
return subject; | ||
} | ||
|
||
const object = JSON.parse(JSON.stringify(subject)); | ||
return recursivelySetValueInObject(object, value, resolveObjectPath(path)); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import {expect} from 'chai'; | ||
|
||
import $set from './index.js'; | ||
|
||
describe('Migrations > Atoms > $set', () => { | ||
describe('Common', () => { | ||
it('$set :: String -> * -> Object -> Object', () => { | ||
expect($set('')).to.be.a('function'); | ||
expect($set('')(NaN)).to.be.a('function'); | ||
expect($set('')(NaN)({})).not.to.be.a('function'); | ||
}); | ||
|
||
it('$set :: (String, *) -> Object -> Object', () => { | ||
expect($set('', NaN)).to.be.a('function'); | ||
expect($set('', NaN)({})).not.to.be.a('function'); | ||
}); | ||
|
||
it('$set :: (String, *, Object) -> Object', () => { | ||
expect($set('', NaN, {})).not.to.be.a('function'); | ||
}); | ||
}) | ||
|
||
describe('Vanilla JS', () => { | ||
it('should shallowly set a value inside an object', () => { | ||
const subject = { | ||
test: 'a' | ||
}; | ||
|
||
expect($set('test', 'b', subject)).to.deep.equal({ | ||
test: 'b' | ||
}); | ||
expect($set('test', 'b', subject)).to.not.equal(subject); | ||
}); | ||
|
||
it('should shallowly set a value inside an array', () => { | ||
const subject = [1, 2, 3]; | ||
|
||
expect($set(1, 42, subject)).to.deep.equal([1, 42, 3]); | ||
expect($set(1, 42, subject)).to.not.equal(subject); | ||
}); | ||
|
||
it('should deeply set a value inside an object', () => { | ||
const subject = { | ||
a: { | ||
b: { | ||
c: { | ||
d: { | ||
e: 'Former Value' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
expect($set('a.b.c.d.e', 'New Value', subject)).to.deep.equal({ | ||
a: { | ||
b: { | ||
c: { | ||
d: { | ||
e: 'New Value' | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
expect($set('a.b.c.d.e', 'New Value', subject)).to.not.equal(subject); | ||
}); | ||
|
||
it('should deeply set a value inside an array', () => { | ||
const subject = [ | ||
[ | ||
[ | ||
[ | ||
['Former Value'] | ||
] | ||
] | ||
] | ||
]; | ||
|
||
expect($set('0.0.0.0.0', 'New Value', subject)).to.deep.equal([ | ||
[ | ||
[ | ||
[ | ||
['New Value'] | ||
] | ||
] | ||
] | ||
]); | ||
expect($set('0.0.0.0.0', 'New Value', subject)).to.not.equal(subject); | ||
}); | ||
|
||
it('should deeply set a value inside a mixed object/array structure', () => { | ||
const subject = { | ||
a: ['b', { | ||
c: { | ||
d: ['e', 'f'] | ||
} | ||
}] | ||
}; | ||
|
||
expect($set('a.0', 'test', subject)).to.deep.equal({ | ||
a: ['test', { | ||
c: { | ||
d: ['e', 'f'] | ||
} | ||
}] | ||
}); | ||
expect($set('a.0', 'test', subject)).to.not.equal(subject); | ||
expect($set('a.1.c', 'test', subject)).to.deep.equal({ | ||
a: ['b', { | ||
c: 'test' | ||
}] | ||
}); | ||
expect($set('a.1.c', 'test', subject)).to.not.equal(subject); | ||
expect($set('a.1.c.d.1', 'test', subject)).to.deep.equal({ | ||
a: ['b', { | ||
c: { | ||
d: ['e', 'test'] | ||
} | ||
}] | ||
}); | ||
expect($set('a.1.c.d.1', 'test', subject)).to.not.equal(subject); | ||
}); | ||
it('should do nothing if the path leads to undefined', () => { | ||
const subject = { | ||
a: ['b', { | ||
c: { | ||
d: ['e', 'f'] | ||
} | ||
}] | ||
}; | ||
|
||
expect($set('a.2', 'test', subject)).to.equal(subject); | ||
expect($set('a.1.c.f', 'test', subject)).to.equal(subject); | ||
expect($set('a.1.c.d.5', 'test', subject)).to.equal(subject); | ||
}); | ||
}); | ||
|
||
describe('Immutable', () => { | ||
it('should shallowly set a value inside an object'); | ||
it('should shallowly set a value inside an array'); | ||
it('should deeply set a value inside an object'); | ||
it('should deeply set a value inside an array'); | ||
it('should deeply set a value inside a mixed object/array structure'); | ||
it('should do nothing if the path leads to undefined'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters