@eramux/rekey
Advanced tools
Comparing version 0.0.10 to 0.0.11
{ | ||
"name": "@eramux/rekey", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "A simple and performant library to rename object keys", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,3 +6,3 @@ // const renamejs = require('../index') | ||
describe("correctly modifies keys of objects", () => { | ||
describe("recursive key traversal", () => { | ||
let testObject: any = {} | ||
@@ -14,2 +14,3 @@ beforeEach(() => { | ||
age: 32, | ||
failure: null, | ||
settings: { | ||
@@ -20,2 +21,3 @@ color: "#ffaabb", | ||
rooms: [ | ||
null, | ||
{ | ||
@@ -52,40 +54,22 @@ name: "Deluxe", | ||
renameKey(testObject, "test.rooms.name", "room_name") | ||
expect(testObject.test.rooms[0].room_name).toEqual("Deluxe") | ||
expect(testObject.test.rooms[1].room_name).toEqual("Deluxe") | ||
}) | ||
}) | ||
describe("correctly deletes keys of objects", () => { | ||
let testObject: any = {} | ||
beforeEach(() => { | ||
testObject = { | ||
test: { | ||
name: "test", | ||
age: 32, | ||
settings: { | ||
color: "#ffaabb", | ||
names: ["ak", "cthulu"] | ||
}, | ||
rooms: [ | ||
{ | ||
name: "Deluxe", | ||
price: 123.32 | ||
}, | ||
{ | ||
name: "Economy", | ||
price: 10, | ||
broken: true, | ||
guests: [ | ||
{ | ||
name: "John" | ||
}, | ||
{ | ||
name: "Doe", | ||
age: 123 | ||
}, | ||
"strage value hmmmmm..." | ||
] | ||
} | ||
] | ||
it("stops the recursion if object does not have the selector property", () => { | ||
let referenceObject = Object.assign({}, testObject) | ||
renameKey(testObject, "test.cthulu.name", "main") | ||
expect(testObject).toEqual(referenceObject) | ||
}) | ||
it("stops the recurstion if a selector paht element has a null value", () => { | ||
let referenceObject = Object.assign({}, testObject) | ||
renameKey(testObject, "test.failure.name", "street") | ||
expect(testObject).toEqual(referenceObject) | ||
}) | ||
it("modifies first level array elements", () => { | ||
let arrayTestObject: any = [ | ||
{ | ||
arraytest: "hello" | ||
} | ||
} | ||
] | ||
renameKey(arrayTestObject, "arraytest", "arraytest_replace") | ||
expect(arrayTestObject[0].arraytest_replace).toEqual("hello") | ||
}) | ||
@@ -99,17 +83,15 @@ it("deltes object keys", () => { | ||
describe("recursive key traversal", () => { | ||
it("ignores null values", () => { | ||
let test = { | ||
item: { | ||
user: null | ||
} | ||
} | ||
// it("ignores null values", () => { | ||
// let test = { | ||
// item: { | ||
// user: null | ||
// } | ||
// } | ||
let result = recursiveKeyTraversal(test, ["item", "user", "username"], (object, key) => { | ||
console.log(object, key) | ||
return true | ||
}) | ||
expect(result).toBe(false) | ||
}) | ||
// let result = recursiveKeyTraversal(test, ["item", "user", "username"], (object, key) => { | ||
// console.log(object, key) | ||
// return true | ||
// }) | ||
// expect(result).toBe(false) | ||
// }) | ||
// it("ignores null values in arrays", () => { | ||
@@ -116,0 +98,0 @@ // let test = { |
@@ -1,2 +0,2 @@ | ||
type KeyTraversalCallback = (workingObject: RekeyObject, currentSelector: string) => Boolean; | ||
type KeyTraversalCallback = (workingObject: RekeyObject, currentSelector: string) => void; | ||
export interface RekeyObject { | ||
@@ -12,7 +12,6 @@ [key: string]: any | ||
*/ | ||
export function recursiveRename(workingObject: RekeyObject, selector: Array<string>, value: string): Boolean { | ||
return recursiveKeyTraversal(workingObject, selector, (object, currentSelector) => { | ||
export function recursiveRename(workingObject: RekeyObject, selector: Array<string>, value: string) { | ||
recursiveKeyTraversal(workingObject, selector, (object, currentSelector) => { | ||
object[value] = object[currentSelector] | ||
delete object[currentSelector] | ||
return true | ||
}) | ||
@@ -26,6 +25,5 @@ } | ||
*/ | ||
export function recursiveDelete(workingObject: RekeyObject, selector: Array<string>): Boolean { | ||
return recursiveKeyTraversal(workingObject, selector, (object, currentSelector) => { | ||
export function recursiveDelete(workingObject: RekeyObject, selector: Array<string>) { | ||
recursiveKeyTraversal(workingObject, selector, (object, currentSelector) => { | ||
delete object[currentSelector] | ||
return true | ||
}) | ||
@@ -36,36 +34,50 @@ } | ||
export function recursiveKeyTraversal(workingObject: RekeyObject, selector: Array<string>, callback: KeyTraversalCallback): Boolean { | ||
let currentSelector = selector[0] | ||
if (selector.length > 1) { | ||
if (workingObject.hasOwnProperty(currentSelector)) { | ||
if (workingObject[currentSelector] == null) { | ||
return false | ||
} else if (workingObject[currentSelector] instanceof Array) { | ||
let restSelector = selector.slice(1) | ||
workingObject[currentSelector] = workingObject[currentSelector].map((item: any) => { | ||
if (item == null) { | ||
return item | ||
} else if (item instanceof Object) { | ||
recursiveKeyTraversal(item, restSelector, callback) | ||
} else { | ||
return item | ||
} | ||
return item | ||
}) | ||
} else if (workingObject[currentSelector] instanceof Object) { | ||
return recursiveKeyTraversal(workingObject[currentSelector], selector.slice(1), callback) | ||
} else { | ||
return false | ||
} | ||
} else { | ||
return false | ||
export function recursiveKeyTraversal(workingObject: RekeyObject, selectors: Array<string>, callback: KeyTraversalCallback) { | ||
let currentSelector = selectors[0] | ||
let remainingSelectors = selectors.slice(1) | ||
// Check if the recursion is in the last layer of the selector | ||
if (selectors.length <= 1) { | ||
if (workingObject instanceof Array && workingObject !== null) { | ||
workingObject = workingObject.map((item: any) => { | ||
if (item instanceof Object && item !== null) { | ||
recursiveKeyTraversal(item, selectors, callback) | ||
} | ||
return item | ||
}) | ||
return | ||
} | ||
} else { | ||
if (workingObject.hasOwnProperty(currentSelector)) { | ||
if (workingObject instanceof Object && workingObject !== null) { | ||
return callback(workingObject, currentSelector) | ||
} else { | ||
return false | ||
} | ||
} | ||
return false | ||
if (!workingObject.hasOwnProperty(currentSelector)) { | ||
return | ||
} | ||
let currentElement = workingObject[currentSelector] | ||
// If the current selector terminates at a null value while still not having arrived at the last selector level, we return false | ||
if (currentElement === null) { | ||
return | ||
} | ||
// If we encounter an array at the current selector, we need to loop through every element and apply the recursiveKeyTraversal on each elegible element | ||
if (currentElement instanceof Array) { | ||
currentElement = currentElement.map((item: any) => { | ||
if (item instanceof Object && item !== null) { | ||
recursiveKeyTraversal(item, remainingSelectors, callback) | ||
} | ||
return item | ||
}) | ||
return | ||
} | ||
if (currentElement instanceof Object) { | ||
recursiveKeyTraversal(currentElement, remainingSelectors, callback) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21083
355