Skip to content

feat(parameters): review types and exports #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 22, 2023
1 change: 1 addition & 0 deletions packages/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './config';
export * as ContextExamples from './samples/resources/contexts';
export * as Events from './samples/resources/events';
export * from './types/middy';
export * from './types/utils';
59 changes: 59 additions & 0 deletions packages/commons/src/types/utils.ts
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 };
120 changes: 120 additions & 0 deletions packages/commons/tests/unit/utils.test.ts
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);
});
});
});
48 changes: 38 additions & 10 deletions packages/parameters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,57 +34,85 @@
"import": "./lib/index.js",
"require": "./lib/index.js"
},
"./base/types": {
"import": "./lib/types/BaseProvider.d.ts",
"require": "./lib/types/BaseProvider.d.ts"
},
"./base": {
"import": "./lib/base/index.js",
"require": "./lib/base/index.js"
},
"./ssm/types": {
"import": "./lib/types/SSMProvider.d.ts",
"require": "./lib/types/SSMProvider.d.ts"
},
"./ssm": {
"import": "./lib/ssm/index.js",
"require": "./lib/ssm/index.js"
},
"./secrets/types": {
"import": "./lib/types/SecretsProvider.d.ts",
"require": "./lib/types/SecretsProvider.d.ts"
},
"./secrets": {
"import": "./lib/secrets/index.js",
"require": "./lib/secrets/index.js"
},
"./dynamodb/types": {
"import": "./lib/types/AppConfigProvider.d.ts",
"require": "./lib/types/AppConfigProvider.d.ts"
},
"./dynamodb": {
"import": "./lib/dynamodb/index.js",
"require": "./lib/dynamodb/index.js"
},
"./appconfig/types": {
"import": "./lib/appconfig/index.js",
"require": "./lib/appconfig/index.js"
},
"./appconfig": {
"import": "./lib/appconfig/index.js",
"require": "./lib/appconfig/index.js"
},
"./errors": {
"import": "./lib/Exceptions.js",
"require": "./lib/Exceptions.js"
},
"./types": {
"import": "./lib/types/index.d.ts",
"require": "./lib/types/index.d.ts"
"import": "./lib/errors.js",
"require": "./lib/errors.js"
}
},
"typesVersions": {
"*": {
"base/types": [
"lib/types/BaseProvider.d.ts"
],
"base": [
"lib/base/index.d.ts"
],
"ssm/types": [
"lib/types/SSMProvider.d.ts"
],
"ssm": [
"lib/ssm/index.d.ts"
],
"secrets/types": [
"lib/types/SecretsProvider.d.ts"
],
"secrets": [
"lib/secrets/index.d.ts"
],
"dynamodb/types": [
"./lib/types/DynamoDBProvider.d.ts"
],
"dynamodb": [
"lib/dynamodb/index.d.ts"
],
"appconfig/types": [
"./lib/types/AppConfigProvider.d.ts"
],
"appconfig": [
"lib/appconfig/index.d.ts"
],
"errors": [
"lib/Exceptions.d.ts"
],
"types": [
"lib/types/index.d.ts"
"lib/errors.d.ts"
]
}
},
Expand Down
Loading