Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
egorovsa committed Jan 22, 2022
1 parent aa30e0a commit 18f45b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"test": "jest",
"build": "./production",
"prepare": "npm run test && npm run build",
"prepare": "git stash -m \"Last changes\" && npm run test && npm run build",
"copyBuild": "cp ./build/* ./",
"prepublishOnly": "npm run copyBuild",
"postpublish": "git clean -df"
Expand Down
50 changes: 25 additions & 25 deletions src/index.ts
@@ -1,38 +1,38 @@
interface JsonData {
[key: string]: string | number | boolean;
[key: string]: string | number | boolean;
}

function isPathIncludesIntoLongerOne(json: JsonData, key: string) {
return Object.keys(json).some(
(keyItem) => keyItem.includes(key) && key !== keyItem
);
return Object.keys(json).some(
(keyItem) => keyItem.includes(key) && key !== keyItem
);
}

export function JsonUnFlat(json: Record<string, string | number | boolean>) {
const unFlatten: Object = {};
const unFlatten: Object = {};

Object.entries(json).forEach(([key, value]) => {
let branchLink: Record<string, any> = unFlatten;
Object.entries(json).forEach(([key, value]) => {
let branchLink: Record<string, any> = unFlatten;

if (isPathIncludesIntoLongerOne(json, key)) {
return;
}
if (isPathIncludesIntoLongerOne(json, key)) {
return;
}

const splittedKey = key.split('.');
const splittedKey = key.split(".");

splittedKey.forEach((splittedItem, index) => {
if (branchLink[splittedItem] === undefined) {
if (splittedKey.length - 1 === index) {
branchLink[splittedItem] = value;
} else {
branchLink[splittedItem] = {};
branchLink = branchLink[splittedItem];
}
} else {
branchLink = branchLink[splittedItem];
}
});
});
splittedKey.forEach((splittedItem, index) => {
if (branchLink[splittedItem] === undefined) {
if (splittedKey.length - 1 === index) {
branchLink[splittedItem] = value;
} else {
branchLink[splittedItem] = {};
branchLink = branchLink[splittedItem];
}
} else {
branchLink = branchLink[splittedItem];
}
});
});

return unFlatten;
return unFlatten;
}

0 comments on commit 18f45b3

Please sign in to comment.