-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parameters): review types and exports (#1528)
* feat: base types * chore: getMultiple void return * wip: ssm/dynamo * chore: add missing await * fix: types import in ssm * feat: imports * feat: imports * tests: coverage * refactor: transformValue * chore: test coverage for commons * docs: typedocs * chore: remove isUint8Array for nodejs14 compat * docs: explained transformValue fn
- Loading branch information
1 parent
d47c3ec
commit 6f96711
Showing
44 changed files
with
1,073 additions
and
668 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,59 @@ | ||
/** | ||
* Returns true if the passed value is a record (object). | ||
* | ||
* @param value | ||
*/ | ||
const isRecord = (value: unknown): value is Record<string, unknown> => { | ||
return ( | ||
Object.prototype.toString.call(value) === '[object Object]' && | ||
!Object.is(value, null) | ||
); | ||
}; | ||
|
||
/** | ||
* Returns true if the passed value is truthy. | ||
* | ||
* @param value | ||
*/ | ||
const isTruthy = (value: unknown): boolean => { | ||
if (typeof value === 'string') { | ||
return value !== ''; | ||
} else if (typeof value === 'number') { | ||
return value !== 0; | ||
} else if (typeof value === 'boolean') { | ||
return value; | ||
} else if (Array.isArray(value)) { | ||
return value.length > 0; | ||
} else if (isRecord(value)) { | ||
return Object.keys(value).length > 0; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
/** | ||
* Returns true if the passed value is null or undefined. | ||
* | ||
* @param value | ||
*/ | ||
const isNullOrUndefined = (value: unknown): value is null | undefined => { | ||
return Object.is(value, null) || Object.is(value, undefined); | ||
}; | ||
|
||
/** | ||
* Returns true if the passed value is a string. | ||
* @param value | ||
* @returns | ||
*/ | ||
const isString = (value: unknown): value is string => { | ||
return typeof value === 'string'; | ||
}; | ||
|
||
export { isRecord, isString, isTruthy, isNullOrUndefined }; | ||
|
||
type JSONPrimitive = string | number | boolean | null | undefined; | ||
type JSONValue = JSONPrimitive | JSONObject | JSONArray; | ||
type JSONObject = { [key: string]: JSONValue }; | ||
type JSONArray = Array<JSONValue>; | ||
|
||
export type { JSONPrimitive, JSONValue, JSONObject, JSONArray }; |
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,120 @@ | ||
/** | ||
* Test utils functions | ||
* | ||
* @group unit/commons/utils | ||
*/ | ||
import { | ||
isRecord, | ||
isTruthy, | ||
isNullOrUndefined, | ||
isString, | ||
} from '../../src/types/utils'; | ||
|
||
describe('Functions: utils', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.resetModules(); | ||
}); | ||
|
||
describe('Function: isRecord', () => { | ||
it('returns true when the passed object is a Record', () => { | ||
// Prepare | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
|
||
// Act | ||
const result = isRecord(obj); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false when the passed object is not a Record', () => { | ||
// Prepare | ||
const obj = [1, 2, 3]; | ||
|
||
// Act | ||
const result = isRecord(obj); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Function: isTruthy', () => { | ||
it.each(['hello', 1, true, [1], { foo: 1 }])( | ||
'returns true when the passed value is truthy', | ||
(testValue) => { | ||
// Prepare | ||
const value = testValue; | ||
|
||
// Act | ||
const result = isTruthy(value); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
} | ||
); | ||
|
||
it.each(['', 0, false, [], {}, Symbol])( | ||
'returns true when the passed value is falsy', | ||
(testValue) => { | ||
// Prepare | ||
const value = testValue; | ||
|
||
// Act | ||
const result = isTruthy(value); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
} | ||
); | ||
}); | ||
|
||
describe('Function: isNullOrUndefined', () => { | ||
it('returns true when the passed value is null or undefined', () => { | ||
// Prepare | ||
const value = undefined; | ||
|
||
// Act | ||
const result = isNullOrUndefined(value); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false when the passed value is not null or undefined', () => { | ||
// Prepare | ||
const value = 'hello'; | ||
|
||
// Act | ||
const result = isNullOrUndefined(value); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Function: isString', () => { | ||
it('returns true when the passed value is a string', () => { | ||
// Prepare | ||
const value = 'hello'; | ||
|
||
// Act | ||
const result = isString(value); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false when the passed value is not a string', () => { | ||
// Prepare | ||
const value = 123; | ||
|
||
// Act | ||
const result = isString(value); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.