Skip to content

Commit

Permalink
fix(lens): fix false values from short circuiting with nullish
Browse files Browse the repository at this point in the history
fix #33
  • Loading branch information
Flavio Corpa committed Oct 4, 2020
1 parent 8f7f2f9 commit 65f8b41
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
"plugins": [
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-transform-modules-commonjs"
]
}
}
}
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parserOptions:
ecmaVersion: 12
sourceType: module
rules:
no-unused-vars: warn
simple-import-sort/sort: warn
parser: '@babel/eslint-parser'
plugins:
Expand Down
10 changes: 9 additions & 1 deletion __tests__/operations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const theme = {
},
}
const fontLense = path(['styles', 'CodeSurfer', 'code', 'fontFamily'])

const themeWithoutFontFamily = {
styles: {
CodeSurfer: {
Expand Down Expand Up @@ -66,6 +65,15 @@ describe('Operations over Optics', () => {
expect(view(fontLense, newTheme)).toBe('MONOSPACED')
})

test('over should work with boolean functions', () => {
const neg = x => !x
const liked = { liked: true }
const lens = optic('liked')

expect(view(lens, liked)).toBe(true)
expect(view(lens, over(lens, neg, liked))).toBe(false)
})

test('path should create the same lense as a manually defined one', () => {
const hardLense = optic(
alter('styles'),
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@babel/core": "^7.11.6",
"@babel/eslint-parser": "^7.11.5",
"@babel/eslint-plugin": "^7.11.5",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
Expand Down Expand Up @@ -99,7 +100,7 @@
"jest": {
"coverageThreshold": {
"global": {
"branches": 98,
"branches": 100,
"functions": 95,
"lines": 100,
"statements": 97
Expand Down
2 changes: 1 addition & 1 deletion src/Lens.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const mustBePresent = key => lens(get(key), set(key))
// alter : String → Lens (Maybe s) (Maybe a)
export const alter = key =>
lens(
obj => (isNotFound(obj) ? notFound : obj[key] || notFound),
obj => (isNotFound(obj) ? notFound : obj[key] ?? notFound),
(val, obj) => {
if (isNotFound(val)) {
if (typeof obj === 'object') {
Expand Down
4 changes: 2 additions & 2 deletions src/Optional.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export const optional = curry((preview, set) => new Optional(preview, set))
// optionalProp : String → Optional s a
export const optionalProp = key =>
optional(
obj => obj[key] || notFound,
obj => obj[key] ?? notFound,
(val, obj) => (obj[key] ? { ...obj, [key]: val } : obj),
)

// optionalIx : Number → Optional s a
export const optionalIx = index =>
optional(
obj => obj[index] || notFound,
obj => obj[index] ?? notFound,
(val, obj) => (obj[index] ? setIndex(index, val, obj) : obj),
)

Expand Down

0 comments on commit 65f8b41

Please sign in to comment.