@aws-lambda-powertools/idempotency
Version:
The idempotency package for the Powertools for AWS Lambda (TypeScript) library. It provides options to make your Lambda functions idempotent and safe to retry.
40 lines (39 loc) • 1.58 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepSort = void 0;
const commons_1 = require("@aws-lambda-powertools/commons");
/**
* Sorts the keys of a provided object in a case-insensitive manner.
*
* This function takes an object as input, sorts its keys alphabetically without
* considering case sensitivity and recursively sorts any nested objects or arrays.
*
* @param {JSONObject} object - The JSON object to be sorted.
* @returns {JSONObject} - A new JSON object with all keys sorted alphabetically in a case-insensitive manner.
*/
const sortObject = (object) => Object.keys(object)
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.reduce((acc, key) => {
acc[key] = deepSort(object[key]);
return acc;
}, {});
/**
* Recursively sorts the keys of an object or elements of an array.
*
* This function sorts the keys of any JSON in a case-insensitive manner and recursively applies the same sorting to
* nested objects and arrays. Primitives (strings, numbers, booleans, null) are returned unchanged.
*
* @param {JSONValue} data - The input data to be sorted, which can be an object, array or primitive value.
* @returns {JSONValue} - The sorted data, with all object's keys sorted alphabetically in a case-insensitive manner.
*/
const deepSort = (data) => {
const type = (0, commons_1.getType)(data);
if (type === 'object') {
return sortObject(data);
}
if (type === 'array') {
return data.map(deepSort);
}
return data;
};
exports.deepSort = deepSort;
;