@pixpilot/object 1.0.2 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/manipulation.d.ts +195 -0
- package/dist/type-guards.d.ts +46 -0
- package/package.json +9 -5
- package/dist/keys-to-camel-case.cjs +0 -1
- package/dist/keys-to-camel-case.js +0 -1
- package/dist/keys-to-snake-case.cjs +0 -1
- package/dist/keys-to-snake-case.js +0 -1
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
1
|
# @pixpilot/object
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A collection of utility functions for working with JavaScript objects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @pixpilot/object
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {
|
|
15
|
+
deepClone,
|
|
16
|
+
isObject,
|
|
17
|
+
keysToCamelCase,
|
|
18
|
+
keysToSnakeCase,
|
|
19
|
+
omit,
|
|
20
|
+
pick,
|
|
21
|
+
} from '@pixpilot/object';
|
|
22
|
+
|
|
23
|
+
// Convert object keys
|
|
24
|
+
const obj = { user_name: 'john', user_details: { first_name: 'John' } };
|
|
25
|
+
const camelCaseObj = keysToCamelCase(obj);
|
|
26
|
+
// { userName: 'john', userDetails: { firstName: 'John' } }
|
|
27
|
+
|
|
28
|
+
// Manipulate objects
|
|
29
|
+
const picked = pick({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { a: 1, c: 3 }
|
|
30
|
+
const omitted = omit({ a: 1, b: 2, c: 3 }, ['b']); // { a: 1, c: 3 }
|
|
31
|
+
|
|
32
|
+
// Clone deeply
|
|
33
|
+
const cloned = deepClone({ a: { b: { c: 1 } } });
|
|
34
|
+
|
|
35
|
+
// Type guards
|
|
36
|
+
if (isObject(value)) {
|
|
37
|
+
// value is typed as Record<string, unknown>
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### Type Guards
|
|
44
|
+
|
|
45
|
+
- `isObject(value: unknown): value is Record<string, unknown>` - Check if value is an object
|
|
46
|
+
- `isPlainObject(value: unknown): value is Record<string, unknown>` - Check if value is a plain object
|
|
47
|
+
- `isEmptyObject(value: unknown): boolean` - Check if object is empty
|
|
48
|
+
|
|
49
|
+
### Key Transformation
|
|
50
|
+
|
|
51
|
+
- `keysToCamelCase<T>(obj: T): KeysToCamelCase<T>` - Convert all keys to camelCase
|
|
52
|
+
- `keysToSnakeCase<T>(obj: T): KeysToSnakeCase<T>` - Convert all keys to snake_case
|
|
53
|
+
|
|
54
|
+
### Object Manipulation
|
|
55
|
+
|
|
56
|
+
- `pick<T, K>(obj: T, keys: K[]): Pick<T, K>` - Pick specific keys from object
|
|
57
|
+
- `omit<T, K>(obj: T, keys: K[]): Omit<T, K>` - Omit specific keys from object
|
|
58
|
+
- `merge<T, U>(target: T, source: U): T & U` - Deep merge two objects
|
|
59
|
+
- `get<T>(obj: object, path: string, defaultValue?: T): T | undefined` - Get nested value by path
|
|
60
|
+
- `set<T>(obj: T, path: string, value: unknown): T` - Set nested value by path
|
|
61
|
+
- `has(obj: object, path: string): boolean` - Check if nested path exists
|
|
62
|
+
- `flatKeys(obj: object, prefix?: string): string[]` - Get all keys including nested paths
|
|
63
|
+
- `deepClone<T>(obj: T): T` - Deep clone an object
|
|
64
|
+
- `deepEqual(obj1: unknown, obj2: unknown): boolean` - Deep equality comparison
|
|
65
|
+
- `mapValues<T, U>(obj: T, fn: (value, key) => U): Record<keyof T, U>` - Map over object values
|
|
66
|
+
- `mapKeys<T>(obj: T, fn: (key) => string): Record<string, T[keyof T]>` - Map over object keys
|
|
67
|
+
|
|
68
|
+
## Contributing
|
|
69
|
+
|
|
70
|
+
We welcome contributions! Please see the [main contributing guide](../../CONTRIBUTING.md) for details.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
This project is licensed under the MIT License. See the [LICENSE](../../LICENSE) file for details.
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";const e=/([\p{Ll}\d])(\p{Lu})/gu,t=/(\p{Lu})([\p{Lu}][\p{Ll}])/gu,r=/(\d)\p{Ll}|(\p{L})\d/u,n=/[^\p{L}\d]+/giu,o="$1\0$2";function c(r){let c=r.trim();c=c.replace(e,o).replace(t,o),c=c.replace(n,"\0");let s=0,u=c.length;for(;"\0"===c.charAt(s);)s++;if(s===u)return[];for(;"\0"===c.charAt(u-1);)u--;return c.slice(s,u).split(/\0/g)}function s(e){const t=c(e);for(let e=0;e<t.length;e++){const n=t[e],o=r.exec(n);if(o){const r=o.index+(o[1]??o[2]).length;t.splice(e,1,n.slice(0,r),n.slice(r))}}return t}function u(e){return!1===e?e=>e.toLowerCase():t=>t.toLocaleLowerCase(e)}function f(e,t={}){const r=t.split??(t.separateNumbers?s:c),n=t.prefixCharacters??"",o=t.suffixCharacters??"";let u=0,f=e.length;for(;u<e.length;){const t=e.charAt(u);if(!n.includes(t))break;u++}for(;f>u;){const t=f-1,r=e.charAt(t);if(!o.includes(r))break;f=t}return[e.slice(0,u),r(e.slice(u,f)),e.slice(f)]}function i(e){return function(e,t){const[r,n,o]=f(e,t),c=u(t?.locale),s=function(e){return t=>t.toLocaleUpperCase(e)}(t?.locale),i=function(e,t){return(r,n)=>{const o=r[0];return(n>0&&o>="0"&&o<="9"?"_"+o:t(o))+e(r.slice(1))}}(c,s);return r+n.map((e,t)=>0===t?c(e):i(e,t)).join("")+o}(e)}function l(e){return function(e,t){const[r,n,o]=f(e,t);return r+n.map(u(t?.locale)).join(t?.delimiter??" ")+o}(e,{delimiter:"_"})}function p(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var a,y={exports:{}};var g=(a||(a=1,function(e){const t=Object.getPrototypeOf({});function r(){return function(e){return!("object"!=typeof e||null===e||e instanceof RegExp||e instanceof Date)}}function n(e){function n(e){return"constructor"!==e&&"prototype"!==e&&"__proto__"!==e}const o=Object.prototype.propertyIsEnumerable,c=e?.symbols?function(e){const t=Object.keys(e),r=Object.getOwnPropertySymbols(e);for(let n=0,c=r.length;n<c;++n)o.call(e,r[n])&&t.push(r[n]);return t}:Object.keys,s="function"==typeof e?.cloneProtoObject?e.cloneProtoObject:void 0,u="function"==typeof e?.isMergeableObject?e.isMergeableObject:r(),f=e&&"function"==typeof e.mergeArray?e.mergeArray({clone:i,deepmerge:l,getKeys:c,isMergeableObject:u}):function(e,t){const r=e.length,n=t.length;let o=0;const c=new Array(r+n);for(;o<r;++o)c[o]=i(e[o]);for(o=0;o<n;++o)c[o+r]=i(t[o]);return c};function i(e){return u(e)?Array.isArray(e)?function(e){let t=0;const r=e.length,n=new Array(r);for(;t<r;++t)n[t]=i(e[t]);return n}(e):function(e){const r={};if(s&&Object.getPrototypeOf(e)!==t)return s(e);const o=c(e);let u,f,l;for(u=0,f=o.length;u<f;++u)n(l=o[u])&&(r[l]=i(e[l]));return r}(e):e}function l(e,r){const o=Array.isArray(r),p=Array.isArray(e);return"object"!=typeof(a=r)||null===a?r:u(e)?o&&p?f(e,r):o!==p?i(r):function(e,r){const o={},f=c(e),p=c(r);let a,y,g;for(a=0,y=f.length;a<y;++a)n(g=f[a])&&-1===p.indexOf(g)&&(o[g]=i(e[g]));for(a=0,y=p.length;a<y;++a)n(g=p[a])&&(g in e?-1!==f.indexOf(g)&&(s&&u(r[g])&&Object.getPrototypeOf(r[g])!==t?o[g]=s(r[g]):o[g]=l(e[g],r[g])):o[g]=i(r[g]));return o}(e,r):i(r);var a}return e?.all?function(){switch(arguments.length){case 0:return{};case 1:return i(arguments[0]);case 2:return l(arguments[0],arguments[1])}let e;for(let t=0,r=arguments.length;t<r;++t)e=l(e,arguments[t]);return e}:l}e.exports=n,e.exports.default=n,e.exports.deepmerge=n,Object.defineProperty(e.exports,"isMergeableObject",{get:r})}(y)),y.exports);function b(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}const j=p(g)();exports.deepClone=function e(t){if(null===t||"object"!=typeof t)return t;if(t instanceof Date)return new Date(t.getTime());if(t instanceof RegExp)return new RegExp(t.source,t.flags);if(Array.isArray(t))return t.map(t=>e(t));const r={};for(const[n,o]of Object.entries(t))r[n]=e(o);return r},exports.deepEqual=function e(t,r){if(t===r)return!0;if(typeof t!=typeof r)return!1;if(null===t||null===r)return!1;if(t instanceof Date&&r instanceof Date)return t.getTime()===r.getTime();if(Array.isArray(t)&&Array.isArray(r))return t.length===r.length&&t.every((t,n)=>e(t,r[n]));if("object"==typeof t&&"object"==typeof r){const n=Object.keys(t),o=Object.keys(r);return n.length===o.length&&n.every(n=>e(t[n],r[n]))}return!1},exports.deepmerge=j,exports.flatKeys=function e(t,r=""){const n=[];for(const[o,c]of Object.entries(t)){const t=r?`${r}.${o}`:o;b(c)?n.push(...e(c,t)):n.push(t)}return n},exports.get=function(e,t,r){const n=t.split(".");let o=e;for(const e of n){if(null==o||"object"!=typeof o||!(e in o))return r;o=o[e]}return o},exports.has=function(e,t){const r=t.split(".");let n=e;for(const e of r){if(null==n||"object"!=typeof n||!(e in n))return!1;n=n[e]}return!0},exports.isEmptyObject=function(e){return b(e)&&0===Object.keys(e).length},exports.isObject=b,exports.isPlainObject=function(e){if(!b(e))return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype},exports.keysToCamelCase=function e(t){if(Array.isArray(t))return t.map(e);if(null===t||"object"!=typeof t)return t;const r={};for(const[n,o]of Object.entries(t)){r[i(n)]=e(o)}return r},exports.keysToSnakeCase=function e(t){if(Array.isArray(t))return t.map(e);if(null===t||"object"!=typeof t)return t;const r={};for(const[n,o]of Object.entries(t)){r[l(n)]=e(o)}return r},exports.mapKeys=function(e,t){const r={};for(const[n,o]of Object.entries(e)){r[t(n)]=o}return r},exports.mapValues=function(e,t){const r={};for(const[n,o]of Object.entries(e))r[n]=t(o,n);return r},exports.merge=function(e,t){return j(e,t)},exports.mergeAll=function(...e){if(0===e.length)return{};if(1===e.length)return e[0]??{};let t={};for(const r of e)t=j(t,r);return t},exports.omit=function(e,t){const r={...e};for(const e of t)delete r[e];return r},exports.pick=function(e,t){const r={};for(const n of t)n in e&&(r[n]=e[n]);return r},exports.set=function(e,t,r){const n=t.split("."),o=n.pop();if(null==o||0===o.length)return e;let c=e;for(const e of n)e in c&&b(c[e])||(c[e]={}),c=c[e];return c[o]=r,e};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
const t=/([\p{Ll}\d])(\p{Lu})/gu,e=/(\p{Lu})([\p{Lu}][\p{Ll}])/gu,n=/(\d)\p{Ll}|(\p{L})\d/u,r=/[^\p{L}\d]+/giu,o="$1\0$2";function c(n){let c=n.trim();c=c.replace(t,o).replace(e,o),c=c.replace(r,"\0");let u=0,f=c.length;for(;"\0"===c.charAt(u);)u++;if(u===f)return[];for(;"\0"===c.charAt(f-1);)f--;return c.slice(u,f).split(/\0/g)}function u(t){const e=c(t);for(let t=0;t<e.length;t++){const r=e[t],o=n.exec(r);if(o){const n=o.index+(o[1]??o[2]).length;e.splice(t,1,r.slice(0,n),r.slice(n))}}return e}function f(t){return!1===t?t=>t.toLowerCase():e=>e.toLocaleLowerCase(t)}function i(t,e={}){const n=e.split??(e.separateNumbers?u:c),r=e.prefixCharacters??"",o=e.suffixCharacters??"";let f=0,i=t.length;for(;f<t.length;){const e=t.charAt(f);if(!r.includes(e))break;f++}for(;i>f;){const e=i-1,n=t.charAt(e);if(!o.includes(n))break;i=e}return[t.slice(0,f),n(t.slice(f,i)),t.slice(i)]}function s(t){return function(t,e){const[n,r,o]=i(t,e),c=f(e?.locale),u=function(t){return e=>e.toLocaleUpperCase(t)}(e?.locale),s=function(t,e){return(n,r)=>{const o=n[0];return(r>0&&o>="0"&&o<="9"?"_"+o:e(o))+t(n.slice(1))}}(c,u);return n+r.map((t,e)=>0===e?c(t):s(t,e)).join("")+o}(t)}function l(t){return function(t,e){const[n,r,o]=i(t,e);return n+r.map(f(e?.locale)).join(e?.delimiter??" ")+o}(t,{delimiter:"_"})}function a(t){if(Array.isArray(t))return t.map(a);if(null===t||"object"!=typeof t)return t;const e={};for(const[n,r]of Object.entries(t)){e[s(n)]=a(r)}return e}function p(t){if(Array.isArray(t))return t.map(p);if(null===t||"object"!=typeof t)return t;const e={};for(const[n,r]of Object.entries(t)){e[l(n)]=p(r)}return e}function y(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var g,b={exports:{}};var j=(g||(g=1,function(t){const e=Object.getPrototypeOf({});function n(){return function(t){return!("object"!=typeof t||null===t||t instanceof RegExp||t instanceof Date)}}function r(t){function r(t){return"constructor"!==t&&"prototype"!==t&&"__proto__"!==t}const o=Object.prototype.propertyIsEnumerable,c=t?.symbols?function(t){const e=Object.keys(t),n=Object.getOwnPropertySymbols(t);for(let r=0,c=n.length;r<c;++r)o.call(t,n[r])&&e.push(n[r]);return e}:Object.keys,u="function"==typeof t?.cloneProtoObject?t.cloneProtoObject:void 0,f="function"==typeof t?.isMergeableObject?t.isMergeableObject:n(),i=t&&"function"==typeof t.mergeArray?t.mergeArray({clone:s,deepmerge:l,getKeys:c,isMergeableObject:f}):function(t,e){const n=t.length,r=e.length;let o=0;const c=new Array(n+r);for(;o<n;++o)c[o]=s(t[o]);for(o=0;o<r;++o)c[o+n]=s(e[o]);return c};function s(t){return f(t)?Array.isArray(t)?function(t){let e=0;const n=t.length,r=new Array(n);for(;e<n;++e)r[e]=s(t[e]);return r}(t):function(t){const n={};if(u&&Object.getPrototypeOf(t)!==e)return u(t);const o=c(t);let f,i,l;for(f=0,i=o.length;f<i;++f)r(l=o[f])&&(n[l]=s(t[l]));return n}(t):t}function l(t,n){const o=Array.isArray(n),a=Array.isArray(t);return"object"!=typeof(p=n)||null===p?n:f(t)?o&&a?i(t,n):o!==a?s(n):function(t,n){const o={},i=c(t),a=c(n);let p,y,g;for(p=0,y=i.length;p<y;++p)r(g=i[p])&&-1===a.indexOf(g)&&(o[g]=s(t[g]));for(p=0,y=a.length;p<y;++p)r(g=a[p])&&(g in t?-1!==i.indexOf(g)&&(u&&f(n[g])&&Object.getPrototypeOf(n[g])!==e?o[g]=u(n[g]):o[g]=l(t[g],n[g])):o[g]=s(n[g]));return o}(t,n):s(n);var p}return t?.all?function(){switch(arguments.length){case 0:return{};case 1:return s(arguments[0]);case 2:return l(arguments[0],arguments[1])}let t;for(let e=0,n=arguments.length;e<n;++e)t=l(t,arguments[e]);return t}:l}t.exports=r,t.exports.default=r,t.exports.deepmerge=r,Object.defineProperty(t.exports,"isMergeableObject",{get:n})}(b)),b.exports);function O(t){return"object"==typeof t&&null!==t&&!Array.isArray(t)}function h(t){if(!O(t))return!1;const e=Object.getPrototypeOf(t);return null===e||e===Object.prototype}function A(t){return O(t)&&0===Object.keys(t).length}const d=y(j)();function m(t,e){const n={};for(const r of e)r in t&&(n[r]=t[r]);return n}function x(t,e){const n={...t};for(const t of e)delete n[t];return n}function L(t,e){return d(t,e)}function w(...t){if(0===t.length)return{};if(1===t.length)return t[0]??{};let e={};for(const n of t)e=d(e,n);return e}function P(t,e,n){const r=e.split(".");let o=t;for(const t of r){if(null==o||"object"!=typeof o||!(t in o))return n;o=o[t]}return o}function _(t,e,n){const r=e.split("."),o=r.pop();if(null==o||0===o.length)return t;let c=t;for(const t of r)t in c&&O(c[t])||(c[t]={}),c=c[t];return c[o]=n,t}function k(t,e){const n=e.split(".");let r=t;for(const t of n){if(null==r||"object"!=typeof r||!(t in r))return!1;r=r[t]}return!0}function v(t,e=""){const n=[];for(const[r,o]of Object.entries(t)){const t=e?`${e}.${r}`:r;O(o)?n.push(...v(o,t)):n.push(t)}return n}function C(t){if(null===t||"object"!=typeof t)return t;if(t instanceof Date)return new Date(t.getTime());if(t instanceof RegExp)return new RegExp(t.source,t.flags);if(Array.isArray(t))return t.map(t=>C(t));const e={};for(const[n,r]of Object.entries(t))e[n]=C(r);return e}function D(t,e){if(t===e)return!0;if(typeof t!=typeof e)return!1;if(null===t||null===e)return!1;if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(Array.isArray(t)&&Array.isArray(e))return t.length===e.length&&t.every((t,n)=>D(t,e[n]));if("object"==typeof t&&"object"==typeof e){const n=Object.keys(t),r=Object.keys(e);return n.length===r.length&&n.every(n=>D(t[n],e[n]))}return!1}function M(t,e){const n={};for(const[r,o]of Object.entries(t))n[r]=e(o,r);return n}function E(t,e){const n={};for(const[r,o]of Object.entries(t)){n[e(r)]=o}return n}export{C as deepClone,D as deepEqual,d as deepmerge,v as flatKeys,P as get,k as has,A as isEmptyObject,O as isObject,h as isPlainObject,a as keysToCamelCase,p as keysToSnakeCase,E as mapKeys,M as mapValues,L as merge,w as mergeAll,x as omit,m as pick,_ as set};
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
declare const deepMerge: import("@fastify/deepmerge").DeepMergeFn;
|
|
2
|
+
/**
|
|
3
|
+
* Pick specific keys from an object.
|
|
4
|
+
*
|
|
5
|
+
* @param obj - The source object
|
|
6
|
+
* @param keys - The keys to pick
|
|
7
|
+
* @returns A new object with only the specified keys
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
12
|
+
* pick(obj, ['a', 'c']); // { a: 1, c: 3 }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K>;
|
|
16
|
+
/**
|
|
17
|
+
* Omit specific keys from an object.
|
|
18
|
+
*
|
|
19
|
+
* @param obj - The source object
|
|
20
|
+
* @param keys - The keys to omit
|
|
21
|
+
* @returns A new object without the specified keys
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
26
|
+
* omit(obj, ['b']); // { a: 1, c: 3 }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K>;
|
|
30
|
+
/**
|
|
31
|
+
* Deep merge two objects.
|
|
32
|
+
*
|
|
33
|
+
* Uses @fastify/deepmerge for efficient and reliable deep merging.
|
|
34
|
+
*
|
|
35
|
+
* @param target - The target object
|
|
36
|
+
* @param source - The source object to merge into the target
|
|
37
|
+
* @returns A new merged object
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const obj1 = { a: 1, b: { c: 2 } };
|
|
42
|
+
* const obj2 = { b: { d: 3 }, e: 4 };
|
|
43
|
+
* merge(obj1, obj2); // { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function merge<T extends Record<string, unknown>, U extends Record<string, unknown>>(target: T, source: U): T & U;
|
|
47
|
+
/**
|
|
48
|
+
* Deep merge multiple objects.
|
|
49
|
+
*
|
|
50
|
+
* Uses @fastify/deepmerge to merge any number of objects into a single result.
|
|
51
|
+
* Objects are merged from left to right, with later objects overriding earlier ones.
|
|
52
|
+
*
|
|
53
|
+
* @param objects - The objects to merge
|
|
54
|
+
* @returns A new merged object
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const obj1 = { a: 1, b: { c: 2 } };
|
|
59
|
+
* const obj2 = { b: { d: 3 }, e: 4 };
|
|
60
|
+
* const obj3 = { e: 5, f: 6 };
|
|
61
|
+
* mergeAll(obj1, obj2, obj3); // { a: 1, b: { c: 2, d: 3 }, e: 5, f: 6 }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function mergeAll<T extends Record<string, unknown>>(...objects: T[]): Record<string, unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* Re-export the configured deepmerge instance from @fastify/deepmerge.
|
|
67
|
+
*
|
|
68
|
+
* This provides direct access to the deepmerge function for advanced use cases,
|
|
69
|
+
* allowing merging of multiple objects and custom options.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { deepmerge } from '@pixpilot/object';
|
|
74
|
+
* const result = deepmerge({ a: 1 }, { b: 2 }, { c: 3 });
|
|
75
|
+
* // { a: 1, b: 2, c: 3 }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export { deepMerge as deepmerge };
|
|
79
|
+
/**
|
|
80
|
+
* Get a nested value from an object using a path string.
|
|
81
|
+
*
|
|
82
|
+
* @param obj - The source object
|
|
83
|
+
* @param path - The path to the value (e.g., 'a.b.c')
|
|
84
|
+
* @param defaultValue - The default value to return if the path doesn't exist
|
|
85
|
+
* @returns The value at the path or the default value
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const obj = { a: { b: { c: 42 } } };
|
|
90
|
+
* get(obj, 'a.b.c'); // 42
|
|
91
|
+
* get(obj, 'a.b.x', 'default'); // 'default'
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare function get<T = unknown>(obj: Record<string, unknown>, path: string, defaultValue?: T): T | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Set a nested value in an object using a path string.
|
|
97
|
+
*
|
|
98
|
+
* @param obj - The target object
|
|
99
|
+
* @param path - The path to set the value at (e.g., 'a.b.c')
|
|
100
|
+
* @param value - The value to set
|
|
101
|
+
* @returns The modified object
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const obj = { a: { b: {} } };
|
|
106
|
+
* set(obj, 'a.b.c', 42); // { a: { b: { c: 42 } } }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function set<T extends Record<string, unknown>>(obj: T, path: string, value: unknown): T;
|
|
110
|
+
/**
|
|
111
|
+
* Check if an object has a nested property using a path string.
|
|
112
|
+
*
|
|
113
|
+
* @param obj - The source object
|
|
114
|
+
* @param path - The path to check (e.g., 'a.b.c')
|
|
115
|
+
* @returns True if the path exists, false otherwise
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const obj = { a: { b: { c: 42 } } };
|
|
120
|
+
* has(obj, 'a.b.c'); // true
|
|
121
|
+
* has(obj, 'a.b.x'); // false
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export declare function has(obj: Record<string, unknown>, path: string): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Get all keys of an object including nested keys.
|
|
127
|
+
*
|
|
128
|
+
* @param obj - The source object
|
|
129
|
+
* @param prefix - The prefix to prepend to keys (used internally for recursion)
|
|
130
|
+
* @returns An array of all keys including nested paths
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const obj = { a: 1, b: { c: 2, d: { e: 3 } } };
|
|
135
|
+
* flatKeys(obj); // ['a', 'b.c', 'b.d.e']
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function flatKeys(obj: Record<string, unknown>, prefix?: string): string[];
|
|
139
|
+
/**
|
|
140
|
+
* Deep clone an object.
|
|
141
|
+
*
|
|
142
|
+
* @param obj - The object to clone
|
|
143
|
+
* @returns A deep clone of the object
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const obj = { a: 1, b: { c: 2 } };
|
|
148
|
+
* const cloned = deepClone(obj);
|
|
149
|
+
* cloned.b.c = 3;
|
|
150
|
+
* console.log(obj.b.c); // 2 (original unchanged)
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export declare function deepClone<T>(obj: T): T;
|
|
154
|
+
/**
|
|
155
|
+
* Compare two objects for deep equality.
|
|
156
|
+
*
|
|
157
|
+
* @param obj1 - The first object
|
|
158
|
+
* @param obj2 - The second object
|
|
159
|
+
* @returns True if the objects are deeply equal, false otherwise
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* deepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } }); // true
|
|
164
|
+
* deepEqual({ a: 1 }, { a: 2 }); // false
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare function deepEqual(obj1: unknown, obj2: unknown): boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Map over the values of an object.
|
|
170
|
+
*
|
|
171
|
+
* @param obj - The source object
|
|
172
|
+
* @param fn - The mapping function
|
|
173
|
+
* @returns A new object with mapped values
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
178
|
+
* mapValues(obj, (value) => value * 2); // { a: 2, b: 4, c: 6 }
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export declare function mapValues<T extends Record<string, unknown>, U>(obj: T, fn: (value: T[keyof T], key: keyof T) => U): Record<keyof T, U>;
|
|
182
|
+
/**
|
|
183
|
+
* Map over the keys of an object.
|
|
184
|
+
*
|
|
185
|
+
* @param obj - The source object
|
|
186
|
+
* @param fn - The mapping function
|
|
187
|
+
* @returns A new object with mapped keys
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const obj = { a: 1, b: 2 };
|
|
192
|
+
* mapKeys(obj, (key) => key.toUpperCase()); // { A: 1, B: 2 }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export declare function mapKeys<T extends Record<string, unknown>>(obj: T, fn: (key: keyof T) => string): Record<string, T[keyof T]>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard to check if a value is an object (and not null or an array).
|
|
3
|
+
*
|
|
4
|
+
* @param value - The value to check
|
|
5
|
+
* @returns True if the value is an object, false otherwise
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* isObject({}); // true
|
|
10
|
+
* isObject({ key: 'value' }); // true
|
|
11
|
+
* isObject([]); // false
|
|
12
|
+
* isObject(null); // false
|
|
13
|
+
* isObject('string'); // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* Type guard to check if a value is a plain object
|
|
19
|
+
* (created with {} or new Object(), not a class instance).
|
|
20
|
+
*
|
|
21
|
+
* @param value - The value to check
|
|
22
|
+
* @returns True if the value is a plain object, false otherwise
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* isPlainObject({}); // true
|
|
27
|
+
* isPlainObject({ key: 'value' }); // true
|
|
28
|
+
* isPlainObject(new Date()); // false
|
|
29
|
+
* isPlainObject([]); // false
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* Type guard to check if a value is an empty object.
|
|
35
|
+
*
|
|
36
|
+
* @param value - The value to check
|
|
37
|
+
* @returns True if the value is an empty object, false otherwise
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* isEmptyObject({}); // true
|
|
42
|
+
* isEmptyObject({ key: 'value' }); // false
|
|
43
|
+
* isEmptyObject([]); // false
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function isEmptyObject(value: unknown): boolean;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pixpilot/object",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.5",
|
|
5
5
|
"author": "Pixpilot <m.doaie@hotmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -24,18 +24,22 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@
|
|
27
|
+
"@fastify/deepmerge": "^3.1.0",
|
|
28
|
+
"@pixpilot/string": "2.0.5"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
31
|
+
"@manypkg/get-packages": "^3.1.0",
|
|
32
|
+
"@rollup/plugin-alias": "^5.1.1",
|
|
33
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
30
34
|
"@types/node": "^22.18.10",
|
|
31
35
|
"eslint": "^9.37.0",
|
|
32
36
|
"rollup": "^4.52.4",
|
|
33
37
|
"typescript": "^5.9.3",
|
|
38
|
+
"@internal/eslint-config": "0.3.0",
|
|
34
39
|
"@internal/prettier-config": "0.0.1",
|
|
35
40
|
"@internal/rollup-config": "0.1.0",
|
|
36
|
-
"@internal/
|
|
37
|
-
"@internal/vitest-config": "0.1.0"
|
|
38
|
-
"@internal/tsconfig": "0.1.0"
|
|
41
|
+
"@internal/tsconfig": "0.1.0",
|
|
42
|
+
"@internal/vitest-config": "0.1.0"
|
|
39
43
|
},
|
|
40
44
|
"prettier": "@internal/prettier-config",
|
|
41
45
|
"scripts": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var r=require("@pixpilot/string");exports.keysToCamelCase=function e(t){if(Array.isArray(t))return t.map(e);if(null===t||"object"!=typeof t)return t;const o={};for(const[s,i]of Object.entries(t)){o[r.toCamelCase(s)]=e(i)}return o};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{toCamelCase as r}from"@pixpilot/string";function t(o){if(Array.isArray(o))return o.map(t);if(null===o||"object"!=typeof o)return o;const n={};for(const[e,i]of Object.entries(o)){n[r(e)]=t(i)}return n}export{t as keysToCamelCase};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var r=require("@pixpilot/string");exports.keysToSnakeCase=function e(t){if(Array.isArray(t))return t.map(e);if(null===t||"object"!=typeof t)return t;const n={};for(const[o,s]of Object.entries(t)){n[r.toSnakeCase(o)]=e(s)}return n};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{toSnakeCase as r}from"@pixpilot/string";function t(o){if(Array.isArray(o))return o.map(t);if(null===o||"object"!=typeof o)return o;const n={};for(const[e,i]of Object.entries(o)){n[r(e)]=t(i)}return n}export{t as keysToSnakeCase};
|