Skip to content

Commit

Permalink
fix(functions): add isNil and do not depend on nullish coallesce
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio Corpa committed Oct 4, 2020
1 parent ac88c2c commit 388a089
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
1 change: 0 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"plugins": ["@babel/plugin-proposal-nullish-coalescing-operator"],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
Expand Down
12 changes: 11 additions & 1 deletion __tests__/functions.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { curry, get, set, setIndex, toUpper } from '../src/functions'
import { curry, get, isNil, set, setIndex, toUpper } from '../src/functions'

const obj = { foo: 'bar' }
const arr = [1, 2, 3]
Expand All @@ -15,6 +15,16 @@ describe('Function Operators', () => {
expect(toUpper('yeah!')).toBe('YEAH!')
})

test('isNil -> should work better than falsy values', () => {
expect(isNil(null)).toBe(true)
expect(isNil(undefined)).toBe(true)
expect(isNil(0)).toBe(false)
expect(isNil(-0)).toBe(false)
expect(isNil('')).toBe(false)
expect(isNil(NaN)).toBe(false)
expect(isNil(false)).toBe(false)
})

test('get -> should retrieve the value of the property if it exists', () => {
expect(get('foo')(obj)).toBe(obj.foo)
})
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@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
4 changes: 2 additions & 2 deletions src/Lens.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fold } from './Fold'
import { curry, get, set, setIndex } from './functions'
import { curry, get, isNil, set, setIndex } from './functions'
import { getter } from './Getter'
import { isNotFound, notFound } from './notFound'
import { optional } from './Optional'
Expand Down 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) || isNil(obj[key]) ? notFound : obj[key]),
(val, obj) => {
if (isNotFound(val)) {
if (typeof obj === 'object') {
Expand Down
6 changes: 3 additions & 3 deletions src/Optional.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OpticCreationError } from './errors'
import { fold } from './Fold'
import { curry, setIndex } from './functions'
import { curry, isNil, setIndex } from './functions'
import { isNotFound, notFound, notFoundToList } from './notFound'
import { partialGetter } from './PartialGetter'
import { setter } from './Setter'
Expand Down 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 => (isNil(obj[key]) ? notFound : obj[key]),
(val, obj) => (obj[key] ? { ...obj, [key]: val } : obj),
)

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

Expand Down
2 changes: 2 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export const setIndex = curry((index, val, array) => array.map((v, i) => (i == i

// toUpper : String -> String
export const toUpper = str => str.toUpperCase()

export const isNil = x => x === null || x === undefined

0 comments on commit 388a089

Please sign in to comment.