-
-
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.
- Loading branch information
Showing
14 changed files
with
419 additions
and
244 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
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,11 @@ | ||
const ensureArray = (value, defaultValue = []) => { | ||
if (value === undefined || value === null) { | ||
return [].concat(defaultValue); | ||
} | ||
|
||
return Array.isArray(value) ? value : [].concat(value); | ||
}; | ||
|
||
export { | ||
ensureArray, | ||
}; |
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,11 @@ | ||
const ensureBoolean = (value, defaultValue = false) => { | ||
if (value === undefined || value === null) { | ||
return Boolean(defaultValue); | ||
} | ||
|
||
return (typeof value === 'boolean') ? value : Boolean(value); // boolean coercible value | ||
}; | ||
|
||
export { | ||
ensureBoolean, | ||
}; |
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 |
---|---|---|
@@ -1,77 +1,5 @@ | ||
/** | ||
* @param {any} obj The object to inspect. | ||
* @returns {boolean} True if the argument appears to be a plain object. | ||
*/ | ||
const isPlainObject = (obj) => { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return false; | ||
} | ||
|
||
let proto = obj; | ||
while (Object.getPrototypeOf(proto) !== null) { | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
|
||
return Object.getPrototypeOf(obj) === proto; | ||
}; | ||
|
||
const ensureArray = (value, defaultValue = []) => { | ||
if (value === undefined || value === null) { | ||
return [].concat(defaultValue); | ||
} | ||
|
||
return Array.isArray(value) ? value : [].concat(value); | ||
}; | ||
|
||
const ensureBoolean = (value, defaultValue = false) => { | ||
if (value === undefined || value === null) { | ||
return Boolean(defaultValue); | ||
} | ||
|
||
return (typeof value === 'boolean') ? value : Boolean(value); // boolean coercible value | ||
}; | ||
|
||
const ensureNumber = (value, defaultValue = 0) => { | ||
if (value === undefined || value === null) { | ||
return ensureNumber(defaultValue); | ||
} | ||
|
||
value = Number(value); | ||
|
||
if (Number.isNaN(value)) { | ||
return ensureNumber(defaultValue); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const ensureFiniteNumber = (value, defaultValue = 0) => { | ||
value = ensureNumber(value, defaultValue); | ||
|
||
return Number.isFinite(value) ? value : ensureFiniteNumber(defaultValue); | ||
}; | ||
|
||
const ensurePlainObject = (value, defaultValue = {}) => { | ||
if (value === undefined || value === null) { | ||
return ensurePlainObject(defaultValue); | ||
} | ||
|
||
return isPlainObject(value) ? value : ensurePlainObject(defaultValue); | ||
}; | ||
|
||
const ensureString = (value, defaultValue = '') => { | ||
if (value === undefined || value === null) { | ||
return String(defaultValue); | ||
} | ||
|
||
return (typeof value === 'string') ? value : String(value); // string coercible value | ||
}; | ||
|
||
export { | ||
ensureArray, | ||
ensureBoolean, | ||
ensureNumber, | ||
ensureFiniteNumber, | ||
ensurePlainObject, | ||
ensureString, | ||
}; | ||
export * from './array'; | ||
export * from './boolean'; | ||
export * from './number'; | ||
export * from './object'; | ||
export * from './string'; |
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,52 @@ | ||
const ensureNumber = (value, defaultValue = 0) => { | ||
if (value === undefined || value === null) { | ||
return ensureNumber(defaultValue); | ||
} | ||
|
||
value = Number(value); // number coercible value | ||
|
||
if (Number.isNaN(value)) { | ||
return ensureNumber(defaultValue); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
const ensureNegativeNumber = (value, defaultValue = 0) => { | ||
const negativeZero = -0; | ||
|
||
return Math.min(ensureNumber(value, defaultValue), negativeZero); | ||
}; | ||
|
||
const ensurePositiveNumber = (value, defaultValue = 0) => { | ||
const positiveZero = +0; | ||
|
||
return Math.max(ensureNumber(value, defaultValue), positiveZero); | ||
}; | ||
|
||
const ensureFiniteNumber = (value, defaultValue = 0) => { | ||
value = ensureNumber(value, defaultValue); | ||
|
||
return Number.isFinite(value) ? value : ensureFiniteNumber(defaultValue); | ||
}; | ||
|
||
const ensureNegativeFiniteNumber = (value, defaultValue = 0) => { | ||
const negativeZero = -0; | ||
|
||
return Math.min(ensureFiniteNumber(value, defaultValue), negativeZero); | ||
}; | ||
|
||
const ensurePositiveFiniteNumber = (value, defaultValue = 0) => { | ||
const positiveZero = +0; | ||
|
||
return Math.max(ensureFiniteNumber(value, defaultValue), positiveZero); | ||
}; | ||
|
||
export { | ||
ensureNumber, | ||
ensureNegativeNumber, | ||
ensurePositiveNumber, | ||
ensureFiniteNumber, | ||
ensureNegativeFiniteNumber, | ||
ensurePositiveFiniteNumber, | ||
}; |
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,28 @@ | ||
/** | ||
* @param {any} obj The object to inspect. | ||
* @returns {boolean} True if the argument appears to be a plain object. | ||
*/ | ||
const isPlainObject = (obj) => { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return false; | ||
} | ||
|
||
let proto = obj; | ||
while (Object.getPrototypeOf(proto) !== null) { | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
|
||
return Object.getPrototypeOf(obj) === proto; | ||
}; | ||
|
||
const ensurePlainObject = (value, defaultValue = {}) => { | ||
if (value === undefined || value === null) { | ||
return ensurePlainObject(defaultValue); | ||
} | ||
|
||
return isPlainObject(value) ? value : ensurePlainObject(defaultValue); | ||
}; | ||
|
||
export { | ||
ensurePlainObject, | ||
}; |
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,11 @@ | ||
const ensureString = (value, defaultValue = '') => { | ||
if (value === undefined || value === null) { | ||
return String(defaultValue); | ||
} | ||
|
||
return (typeof value === 'string') ? value : String(value); // string coercible value | ||
}; | ||
|
||
export { | ||
ensureString, | ||
}; |
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,25 @@ | ||
import { | ||
ensureArray, | ||
} from '../src'; | ||
|
||
describe('Array', () => { | ||
test('ensureArray', () => { | ||
expect(ensureArray()).toEqual([]); | ||
expect(ensureArray({})).toEqual([{}]); | ||
expect(ensureArray(true)).toEqual([true]); | ||
expect(ensureArray(false)).toEqual([false]); | ||
expect(ensureArray([1, 2, 3])).toEqual([1, 2, 3]); | ||
expect(ensureArray(10)).toEqual([10]); | ||
expect(ensureArray(NaN)).toEqual([NaN]); | ||
expect(ensureArray(undefined)).toEqual([]); | ||
expect(ensureArray(null)).toEqual([]); | ||
expect(ensureArray('')).toEqual(['']); | ||
expect(ensureArray(' ')).toEqual([' ']); | ||
|
||
// Returns the coerced default value. | ||
expect(ensureArray(null, 0)).toEqual([0]); | ||
|
||
// Returns the default value. | ||
expect(ensureArray(null, [0])).toEqual([0]); | ||
}); | ||
}); |
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 { | ||
ensureBoolean, | ||
} from '../src'; | ||
|
||
describe('Boolean', () => { | ||
test('ensureBoolean', () => { | ||
expect(ensureBoolean()).toEqual(false); | ||
expect(ensureBoolean({})).toEqual(true); | ||
expect(ensureBoolean(true)).toEqual(true); | ||
expect(ensureBoolean(false)).toEqual(false); | ||
expect(ensureBoolean(0)).toEqual(false); | ||
expect(ensureBoolean(1)).toEqual(true); | ||
expect(ensureBoolean(2e+64)).toEqual(true); | ||
expect(ensureBoolean(Infinity)).toEqual(true); | ||
expect(ensureBoolean(-Infinity)).toEqual(true); | ||
expect(ensureBoolean(NaN)).toEqual(false); | ||
expect(ensureBoolean(undefined)).toEqual(false); | ||
expect(ensureBoolean(null)).toEqual(false); | ||
expect(ensureBoolean('0')).toEqual(true); | ||
expect(ensureBoolean('1')).toEqual(true); | ||
expect(ensureBoolean('')).toEqual(false); | ||
expect(ensureBoolean(' ')).toEqual(true); | ||
|
||
// Returns the coerced default value. | ||
expect(ensureBoolean(null, '')).toEqual(false); | ||
|
||
// Returns the default value. | ||
expect(ensureBoolean(null, true)).toEqual(true); | ||
}); | ||
}); |
Oops, something went wrong.