Skip to content

Commit 6f96711

Browse files
authoredJun 22, 2023
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
1 parent d47c3ec commit 6f96711

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1073
-668
lines changed
 

‎packages/commons/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './config';
44
export * as ContextExamples from './samples/resources/contexts';
55
export * as Events from './samples/resources/events';
66
export * from './types/middy';
7+
export * from './types/utils';

‎packages/commons/src/types/utils.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Returns true if the passed value is a record (object).
3+
*
4+
* @param value
5+
*/
6+
const isRecord = (value: unknown): value is Record<string, unknown> => {
7+
return (
8+
Object.prototype.toString.call(value) === '[object Object]' &&
9+
!Object.is(value, null)
10+
);
11+
};
12+
13+
/**
14+
* Returns true if the passed value is truthy.
15+
*
16+
* @param value
17+
*/
18+
const isTruthy = (value: unknown): boolean => {
19+
if (typeof value === 'string') {
20+
return value !== '';
21+
} else if (typeof value === 'number') {
22+
return value !== 0;
23+
} else if (typeof value === 'boolean') {
24+
return value;
25+
} else if (Array.isArray(value)) {
26+
return value.length > 0;
27+
} else if (isRecord(value)) {
28+
return Object.keys(value).length > 0;
29+
} else {
30+
return false;
31+
}
32+
};
33+
34+
/**
35+
* Returns true if the passed value is null or undefined.
36+
*
37+
* @param value
38+
*/
39+
const isNullOrUndefined = (value: unknown): value is null | undefined => {
40+
return Object.is(value, null) || Object.is(value, undefined);
41+
};
42+
43+
/**
44+
* Returns true if the passed value is a string.
45+
* @param value
46+
* @returns
47+
*/
48+
const isString = (value: unknown): value is string => {
49+
return typeof value === 'string';
50+
};
51+
52+
export { isRecord, isString, isTruthy, isNullOrUndefined };
53+
54+
type JSONPrimitive = string | number | boolean | null | undefined;
55+
type JSONValue = JSONPrimitive | JSONObject | JSONArray;
56+
type JSONObject = { [key: string]: JSONValue };
57+
type JSONArray = Array<JSONValue>;
58+
59+
export type { JSONPrimitive, JSONValue, JSONObject, JSONArray };

0 commit comments

Comments
 (0)
Please sign in to comment.