-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Modules): Add lens along with assocPath
- Loading branch information
1 parent
19560ed
commit fcfaec6
Showing
15 changed files
with
228 additions
and
25 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
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,29 @@ | ||
import assocPath from '../../modules/assocPath' | ||
|
||
describe('Associate Path', () => { | ||
test('associates a value to a nested path', () => { | ||
const data = { | ||
a: { | ||
b: { | ||
c: 1 | ||
} | ||
}, | ||
d: 1 | ||
} | ||
|
||
const path = ['a', 'b', 'c'] | ||
const value = 2 | ||
const expected = { | ||
a: { | ||
b: { | ||
c: 2 | ||
} | ||
}, | ||
d: 1 | ||
} | ||
|
||
const actual = assocPath(path, value, data) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
}) |
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,27 @@ | ||
import over from '../../modules/over' | ||
import lensProp from '../../modules/lensProp' | ||
|
||
describe('Over', () => { | ||
test('It sets the value at the given lens by applying the given function', () => { | ||
const data = { | ||
name: 'Tim', | ||
age: 29 | ||
} | ||
|
||
const nameLens = lensProp('name') | ||
|
||
const setName = name => { | ||
expect(name).toBe('Tim') | ||
return 'John' | ||
} | ||
|
||
const expected = { | ||
name: 'John', | ||
age: 29 | ||
} | ||
|
||
const actual = over(nameLens, setName, data) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
}) |
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,22 @@ | ||
import set from '../../modules/set' | ||
import lensProp from '../../modules/lensProp' | ||
|
||
describe('Set', () => { | ||
test('It sets the given value on the given lens in the given data', () => { | ||
const data = { | ||
name: 'Tim', | ||
age: 29 | ||
} | ||
|
||
const nameLens = lensProp('name') | ||
|
||
const expected = { | ||
name: 'John', | ||
age: 29 | ||
} | ||
|
||
const actual = set(nameLens, 'John', data) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
}) |
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,17 @@ | ||
import view from '../../modules/view' | ||
import lensProp from '../../modules/lensProp' | ||
|
||
describe('View', () => { | ||
test('It returns the value at the lens', () => { | ||
const data = { | ||
name: 'Tim', | ||
age: 29 | ||
} | ||
|
||
const nameLens = lensProp('name') | ||
const expected = 'Tim' | ||
const actual = view(nameLens, data) | ||
|
||
expect(actual).toBe(expected) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import curry from './curry' | ||
import clone from './clone' | ||
import cloneType from './cloneType' | ||
import assoc from './assoc' | ||
import prop from './prop' | ||
|
||
/** | ||
* Associates a value at a path | ||
* | ||
* @example | ||
* | ||
* assocPath(['profile', 'age'], 30, { profile: { age: 29 } } ) === { profile: { age: 30 } } | ||
* | ||
* | ||
* @param {Array<string|number>} key - The path to associate | ||
* @param {*} value - The value to associate | ||
* @param {Object} obj - The structure to associate with | ||
* @return {Object} | ||
*/ | ||
export const assocPath = (targetPath, value, data) => { | ||
// Ensure we are not mutating anything | ||
const path = targetPath.slice() | ||
|
||
// If we are at the end of our recursion | ||
if (path.length < 2) { | ||
return assoc(targetPath.shift(), value, data) | ||
} | ||
|
||
const key = path.shift() | ||
|
||
// Associate the top key with the recursive result | ||
return assoc(key, assocPath(path, value, prop(key, data)), data) | ||
} | ||
|
||
export default curry(assocPath) |
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,7 @@ | ||
export const cloneType = base => { | ||
if (Array.isArray(base)) { | ||
return [] | ||
} | ||
|
||
return {} | ||
} |
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,14 @@ | ||
import curry from './curry' | ||
|
||
/** | ||
* A way to get and set values in a data structure | ||
* | ||
* @param {function(any) => any} get | ||
* @param {function(any, a) => a} set | ||
*/ | ||
export const lens = (get, set) => ({ | ||
get, | ||
set | ||
}) | ||
|
||
export default curry(lens) |
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,14 @@ | ||
import curry from './curry' | ||
import path from './path' | ||
import lens from './lens' | ||
import assocPath from './assocPath' | ||
|
||
/** | ||
* A way to create a lens for a given path | ||
* | ||
* @param {Array<string|number>} keys - The path to the data we care about | ||
* @returns {Lens} - A lens to the Path | ||
*/ | ||
export const lensPath = keys => lens(path(keys), assocPath(keys)) | ||
|
||
export default curry(lensPath) |
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,12 @@ | ||
import curry from './curry' | ||
import lensPath from './lensPath' | ||
|
||
/** | ||
* Creates a lens at a given path | ||
* | ||
* @param {string|number} key - The key to associate this lens with | ||
* @return {Lens} | ||
*/ | ||
export const lensProp = key => lensPath([key]) | ||
|
||
export default curry(lensProp) |
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,15 @@ | ||
import curry from './curry' | ||
|
||
/** | ||
* A way to set a value on a lens via a function | ||
* The function gets passed the value at the key along with the | ||
* data structure | ||
* | ||
* @param {Lens} lens - The lens to the value to set | ||
* @param {function(*, *) => *} fn - A function to immutably create a new value | ||
* @param {*} data - The data to look in | ||
* @return {*} | ||
*/ | ||
export const over = (lens, fn, data) => lens.set(fn(lens.get(data)), data) | ||
|
||
export default curry(over) |
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,13 @@ | ||
import curry from './curry' | ||
|
||
/** | ||
* Sets the given value at the given lens | ||
* | ||
* @param {Lens} lens - The lens to the value | ||
* @param {*} value - The value to set at the lens | ||
* @param {*} data - The data structure to look in | ||
* @return {*} - The data with the lens set | ||
*/ | ||
export const set = (lens, value, data) => lens.set(value, data) | ||
|
||
export default curry(set) |
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,12 @@ | ||
import curry from './curry' | ||
|
||
/** | ||
* A way to actually view the value | ||
* | ||
* @param {Lens} lens - The lens to the data | ||
* @param {*} data - The data structure to look in | ||
* @return {*} | ||
*/ | ||
export const view = (lens, data) => lens.get(data) | ||
|
||
export default curry(view) |
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