nestify-anything
Advanced tools
Comparing version 2.0.5 to 3.0.0
/** | ||
* creates an object from a path | ||
* | ||
* @param {string} path a.path.like.this | ||
* @param {unknown} payload the value to attach to the nested prop | ||
* @returns {Record<string, unknown>} eg. `{a: {path: {like: {this: 'payload'}}}}` | ||
* @param path a.path.like.this | ||
* @param payload the value to attach to the nested prop | ||
* @returns eg. `{a: {path: {like: {this: 'payload'}}}}` | ||
*/ | ||
declare function createObjectFromPath(path: string, payload: unknown): Record<string, unknown>; | ||
export declare function createObjectFromPath(path: string, payload: unknown): { | ||
[key in string]: unknown; | ||
}; | ||
/** | ||
* Recreates an object from any `nested.props` in a passed target object. | ||
* | ||
* @param {Record<string, unknown>} payload object with flat prop paths - eg. `{ 'size.h': 0, 'size.w': 0 }` | ||
* @returns {Record<string, unknown>} object with nested props - eg. `{ size: { h: 0, w: 0 } }` | ||
* @param payload object with flat prop paths - eg. `{ 'size.h': 0, 'size.w': 0 }` | ||
* @returns object with nested props - eg. `{ size: { h: 0, w: 0 } }` | ||
* @example | ||
@@ -19,4 +21,6 @@ * const result = nestifyObject({ 'size.h': 0, 'size.w': 0 }) | ||
*/ | ||
declare function nestifyObject(payload: Record<string, unknown>): Record<string, unknown>; | ||
export { createObjectFromPath, nestifyObject }; | ||
export declare function nestifyObject(payload: { | ||
[key in string]: unknown; | ||
}): { | ||
[key in string]: unknown; | ||
}; |
import { merge } from 'merge-anything'; | ||
function createObjectFromPath(path, payload) { | ||
if (!path.includes(".")) | ||
return { [path]: payload }; | ||
const newValue = payload; | ||
const result = {}; | ||
const matches = Array.from(path.matchAll(/[^.]+/g), ([x]) => x); | ||
let point = result; | ||
let index = 0; | ||
for (const _prop of matches) { | ||
const prop = _prop.replace(/_____dot_____/g, "."); | ||
const isLast = index++ === matches.length - 1; | ||
const container = isLast ? newValue : {}; | ||
point[prop] = container; | ||
point = container; | ||
} | ||
return result; | ||
/** | ||
* creates an object from a path | ||
* | ||
* @param path a.path.like.this | ||
* @param payload the value to attach to the nested prop | ||
* @returns eg. `{a: {path: {like: {this: 'payload'}}}}` | ||
*/ | ||
export function createObjectFromPath(path, payload) { | ||
// edge cases | ||
if (!path.includes('.')) | ||
return { [path]: payload }; | ||
// start | ||
const newValue = payload; | ||
// important to set the result here and not return the reduce directly! | ||
const result = {}; | ||
const matches = Array.from(path.matchAll(/[^.]+/g), ([x]) => x); | ||
let point = result; | ||
let index = 0; | ||
for (const _prop of matches) { | ||
const prop = _prop.replace(/_____dot_____/g, '.'); | ||
const isLast = index++ === matches.length - 1; | ||
const container = isLast ? newValue : {}; | ||
point[prop] = container; | ||
point = container; | ||
} | ||
return result; | ||
} | ||
function nestifyObject(payload) { | ||
return Object.entries(payload).reduce((carry, [key, value]) => { | ||
const nestedObject = createObjectFromPath(key, value); | ||
return merge(carry, nestedObject); | ||
}, {}); | ||
/** | ||
* Recreates an object from any `nested.props` in a passed target object. | ||
* | ||
* @param payload object with flat prop paths - eg. `{ 'size.h': 0, 'size.w': 0 }` | ||
* @returns object with nested props - eg. `{ size: { h: 0, w: 0 } }` | ||
* @example | ||
* const result = nestifyObject({ 'size.h': 0, 'size.w': 0 }) | ||
* // result is: | ||
* { size: { h: 0, w: 0 } } | ||
*/ | ||
export function nestifyObject(payload) { | ||
return Object.entries(payload).reduce((carry, [key, value]) => { | ||
const nestedObject = createObjectFromPath(key, value); | ||
return merge(carry, nestedObject); | ||
}, {}); | ||
} | ||
export { createObjectFromPath, nestifyObject }; |
{ | ||
"name": "nestify-anything", | ||
"version": "2.0.5", | ||
"version": "3.0.0", | ||
"description": "Recreates an object from any `nested.props`. A simple and small integration.", | ||
"type": "module", | ||
"sideEffects": false, | ||
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
@@ -22,13 +19,10 @@ ".": { | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=12.13" | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"test": "vitest run", | ||
"lint": "tsc --noEmit && eslint ./src --ext .ts", | ||
"build": "rollup -c ./rollup.config.js", | ||
"release": "npm run lint && del dist && npm run build && np" | ||
"lint": "tsc --noEmit && eslint ./src", | ||
"build": "del-cli dist && tsc", | ||
"release": "npm run lint && npm run build && np" | ||
}, | ||
@@ -39,16 +33,11 @@ "dependencies": { | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^5.59.2", | ||
"@typescript-eslint/parser": "^5.59.2", | ||
"del-cli": "^5.0.0", | ||
"eslint": "^8.40.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-tree-shaking": "^1.10.0", | ||
"np": "^7.7.0", | ||
"prettier": "^2.8.8", | ||
"rollup": "^3.23.0", | ||
"rollup-plugin-dts": "^5.3.0", | ||
"rollup-plugin-esbuild": "^5.0.0", | ||
"typescript": "^5.0.4", | ||
"vitest": "^0.31.0" | ||
"del-cli": "^5.1.0", | ||
"np": "^10.0.5", | ||
"vitest": "^1.6.0", | ||
"@cycraft/eslint": "^0.3.0", | ||
"@cycraft/tsconfig": "^0.1.2" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"keywords": [ | ||
@@ -62,44 +51,11 @@ "nestify", | ||
], | ||
"author": "Luca Ban - Mesqueeb", | ||
"author": "Luca Ban - Mesqueeb (https://cycraft.co)", | ||
"funding": "https://github.com/sponsors/mesqueeb", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mesqueeb/nestify-anything/issues" | ||
}, | ||
"homepage": "https://github.com/mesqueeb/nestify-anything#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mesqueeb/nestify-anything.git" | ||
"url": "https://github.com/mesqueeb/nestify-anything.git" | ||
}, | ||
"np": { | ||
"yarn": false, | ||
"branch": "production" | ||
}, | ||
"eslintConfig": { | ||
"ignorePatterns": [ | ||
"node_modules", | ||
"dist", | ||
"scripts", | ||
"test" | ||
], | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"tree-shaking" | ||
], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off", | ||
"tree-shaking/no-side-effects-in-initialization": "error", | ||
"@typescript-eslint/ban-ts-comment": "off" | ||
} | ||
} | ||
"homepage": "https://github.com/mesqueeb/nestify-anything#readme", | ||
"bugs": "https://github.com/mesqueeb/nestify-anything/issues" | ||
} |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5
0
6602
1298
5
69
1