Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit d2d112e

Browse files
authored
✨ Add lang.and and lang.or for booleans (#220)
* ✨ Add lang.and * ✨ add lang.or
1 parent 2267229 commit d2d112e

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

packages/immutadot/src/lang/and.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { convert } from 'core/convert'
2+
3+
/**
4+
* Applies <code>&&</code> between the former value and <code>args</code>
5+
* @function
6+
* @memberof lang
7+
* @param {Object} object The object to modify.
8+
* @param {Array|string} path The path of the property to set.
9+
* @param {...*} [args] Other operands.
10+
* @return {Object} Returns the updated object.
11+
* @example and({ nested: { prop: true } }, 'nested.prop', true) // { nested: { prop: true } }
12+
* @example and({ nested: { prop: true } }, 'nested.prop', true, false) // { nested: { prop: false } }
13+
* @since 1.0.0
14+
*/
15+
const and = convert((v, ...args) => args.reduce((acc, arg) => acc && arg, v))
16+
17+
export { and }
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-env jest */
2+
import { and } from 'lang'
3+
import { immutaTest } from 'test.utils'
4+
5+
describe('lang.and', () => {
6+
7+
const withTrue = {
8+
nested: { prop: true },
9+
other: {},
10+
}
11+
const withFalse = {
12+
nested: { prop: false },
13+
other: {},
14+
}
15+
16+
it('should stay true', () => {
17+
immutaTest(input => {
18+
const output = and(input, 'nested.prop')
19+
expect(output).toEqual(withTrue)
20+
return output
21+
}, withTrue, 'nested')
22+
})
23+
24+
it('should stay true', () => {
25+
immutaTest(input => {
26+
const output = and(input, 'nested.prop', true)
27+
expect(output).toEqual(withTrue)
28+
return output
29+
}, withTrue, 'nested')
30+
})
31+
32+
it('should set deep undefined to undefined', () => {
33+
immutaTest((input, path) => {
34+
const output = and(input, path)
35+
expect(output).toEqual({ nested: { prop: undefined } })
36+
return output
37+
}, undefined, 'nested.prop')
38+
})
39+
40+
it('should set to false', () => {
41+
immutaTest((input, path) => {
42+
const output = and(input, path, false)
43+
expect(output).toEqual(withFalse)
44+
return output
45+
}, withTrue, 'nested.prop')
46+
})
47+
48+
it('should stay false', () => {
49+
immutaTest(input => {
50+
const output = and(input, 'nested.prop', true)
51+
expect(output).toEqual(withFalse)
52+
return output
53+
}, withFalse, 'nested')
54+
})
55+
56+
it('should set to false', () => {
57+
immutaTest((input, path) => {
58+
const output = and(input, path, true, false)
59+
expect(output).toEqual(withFalse)
60+
return output
61+
}, withTrue, 'nested.prop')
62+
})
63+
})

packages/immutadot/src/lang/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66

77
export { add } from './add'
8+
export { and } from './and'
89
export { divide } from './divide'
910
export { multiply } from './multiply'
11+
export { or } from './or'
1012
export { subtract } from './subtract'
1113
export { toggle } from './toggle'

packages/immutadot/src/lang/or.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { convert } from 'core/convert'
2+
3+
/**
4+
* Applies <code>||</code> between the former value and <code>args</code>
5+
* @function
6+
* @memberof lang
7+
* @param {Object} object The object to modify.
8+
* @param {Array|string} path The path of the property to set.
9+
* @param {...*} [args] Other operands.
10+
* @return {Object} Returns the updated object.
11+
* @example or({ nested: { prop: false } }, 'nested.prop', true) // { nested: { prop: true } }
12+
* @example or({ nested: { prop: true } }, 'nested.prop', false, false) // { nested: { prop: true } }
13+
* @since 1.0.0
14+
*/
15+
const or = convert((v, ...args) => args.reduce((acc, arg) => acc || arg, v))
16+
17+
export { or }
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-env jest */
2+
import { immutaTest } from 'test.utils'
3+
import { or } from 'lang'
4+
5+
describe('lang.or', () => {
6+
7+
const withTrue = {
8+
nested: { prop: true },
9+
other: {},
10+
}
11+
const withFalse = {
12+
nested: { prop: false },
13+
other: {},
14+
}
15+
16+
it('should stay true', () => {
17+
immutaTest(input => {
18+
const output = or(input, 'nested.prop')
19+
expect(output).toEqual(withTrue)
20+
return output
21+
}, withTrue, 'nested')
22+
})
23+
24+
it('should stay true', () => {
25+
immutaTest(input => {
26+
const output = or(input, 'nested.prop', true)
27+
expect(output).toEqual(withTrue)
28+
return output
29+
}, withTrue, 'nested')
30+
})
31+
32+
it('should set deep undefined to undefined', () => {
33+
immutaTest((input, path) => {
34+
const output = or(input, path)
35+
expect(output).toEqual({ nested: { prop: undefined } })
36+
return output
37+
}, undefined, 'nested.prop')
38+
})
39+
40+
it('should stay true', () => {
41+
immutaTest(input => {
42+
const output = or(input, 'nested.prop', false)
43+
expect(output).toEqual(withTrue)
44+
return output
45+
}, withTrue, 'nested')
46+
})
47+
48+
it('should set to true', () => {
49+
immutaTest((input, path) => {
50+
const output = or(input, path, true)
51+
expect(output).toEqual(withTrue)
52+
return output
53+
}, withFalse, 'nested.prop')
54+
})
55+
56+
it('should set to false', () => {
57+
immutaTest((input, path) => {
58+
const output = or(input, path, true, false)
59+
expect(output).toEqual(withTrue)
60+
return output
61+
}, withFalse, 'nested.prop')
62+
})
63+
})

0 commit comments

Comments
 (0)