@bemoje/object 1.0.0 → 2.0.1

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 ADDED
@@ -0,0 +1,172 @@
1
+ # @bemoje/object
2
+
3
+ Comprehensive object manipulation utilities for property definition, traversal, filtering, mapping, prototype
4
+ chains, and descriptor management.
5
+
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5-blue)](https://www.typescriptlang.org/)
7
+ [![Module](https://img.shields.io/badge/Module-ESM-yellow)](https://nodejs.org/api/esm.html)
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @bemoje/object
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Object Assignment
18
+
19
+ ```ts
20
+ import { objAssign } from '@bemoje/object'
21
+
22
+ // Like Object.assign but skips null/undefined values
23
+ objAssign({ a: 1, b: 2 }, { b: null, c: 3 })
24
+ // { a: 1, b: 2, c: 3 }
25
+ ```
26
+
27
+ ### Property Definition
28
+
29
+ ```ts
30
+ import { defineGetter, defineSetter, defineMethod, defineValue, defineLazyProperty } from '@bemoje/object'
31
+
32
+ const obj = {}
33
+
34
+ defineGetter(obj, 'now', () => Date.now())
35
+ defineMethod(obj, 'greet', function () {
36
+ return 'hello'
37
+ })
38
+ defineValue(obj, 'version', '1.0.0')
39
+
40
+ // Lazy property: computed once on first access, then cached
41
+ defineLazyProperty(obj, 'config', () => loadExpensiveConfig())
42
+ ```
43
+
44
+ ### Filter and Map Objects
45
+
46
+ ```ts
47
+ import { filterObject, mapObject, mapObjectKeys, mapObjectEntries } from '@bemoje/object'
48
+
49
+ const data = { a: 1, b: 2, c: 3 }
50
+
51
+ filterObject(data, (value) => value > 1)
52
+ // { b: 2, c: 3 }
53
+
54
+ mapObject(data, (value) => value * 10)
55
+ // { a: 10, b: 20, c: 30 }
56
+
57
+ mapObjectKeys(data, (key) => key.toUpperCase())
58
+ // { A: 1, B: 2, C: 3 }
59
+ ```
60
+
61
+ ### Prototype Chain Inspection
62
+
63
+ ```ts
64
+ import { getPrototypeChain, getClassChain, getSuperClasses } from '@bemoje/object'
65
+
66
+ class Animal {}
67
+ class Dog extends Animal {}
68
+
69
+ getPrototypeChain(new Dog()) // [Dog.prototype, Animal.prototype, Object.prototype, null]
70
+ getClassChain(Dog) // [Animal, Object]
71
+ getSuperClasses(Dog) // [Animal, Object]
72
+ ```
73
+
74
+ ### Inheritance Utilities
75
+
76
+ ```ts
77
+ import { inheritPrototypeMembers, inheritStaticMembers } from '@bemoje/object'
78
+
79
+ // Copy prototype methods from source to target class
80
+ inheritPrototypeMembers(TargetClass, SourceClass, ['excludeMethod'])
81
+ inheritStaticMembers(TargetClass, SourceClass)
82
+ ```
83
+
84
+ ### Deep Operations
85
+
86
+ ```ts
87
+ import { objDeepFreeze, iterateObject } from '@bemoje/object'
88
+
89
+ // Deep freeze an object
90
+ const frozen = objDeepFreeze({ nested: { deep: { value: 42 } } })
91
+
92
+ // Traverse all properties depth-first
93
+ for (const node of iterateObject({ a: { b: [1, 2] } })) {
94
+ console.log(node.propertyPath, node.value, node.isLeaf)
95
+ }
96
+ // 'a' { b: [1, 2] } false
97
+ // 'a.b' [1, 2] false
98
+ // 'a.b[0]' 1 true
99
+ // 'a.b[1]' 2 true
100
+ ```
101
+
102
+ ### Sort, Delete, Reduce
103
+
104
+ ```ts
105
+ import { objSortKeys, objDelete, objReduce, objIsEmpty } from '@bemoje/object'
106
+
107
+ objSortKeys({ c: 3, a: 1, b: 2 }) // { a: 1, b: 2, c: 3 }
108
+ objDelete({ name: 'John', age: 30 }, 'age') // { name: 'John' }
109
+ objReduce({ a: 1, b: 2 }, (acc, val) => acc + val, 0) // 3
110
+ objIsEmpty({}) // true
111
+ ```
112
+
113
+ ### Typed Helpers
114
+
115
+ ```ts
116
+ import { keysOf, valuesOf, entriesOf } from '@bemoje/object'
117
+
118
+ const obj = { name: 'Alice', age: 30 }
119
+ keysOf(obj) // ['name', 'age'] (typed as ('name' | 'age')[])
120
+ valuesOf(obj) // ['Alice', 30]
121
+ entriesOf(obj) // [['name', 'Alice'], ['age', 30]]
122
+ ```
123
+
124
+ ## API Reference
125
+
126
+ | Export | Description |
127
+ | --------------------------- | --------------------------------------------------------- |
128
+ | `objAssign` | Object.assign that skips null/undefined values |
129
+ | `objDeepFreeze` | Deep freeze an object recursively |
130
+ | `objSortKeys` | Sort object keys alphabetically or with custom comparator |
131
+ | `objDelete` | Delete a key and return the object |
132
+ | `objReduce` | Reduce over object entries |
133
+ | `objForEach` | Iterate over object entries |
134
+ | `objIsEmpty` | Check if object has no enumerable keys |
135
+ | `objSize` | Count enumerable keys |
136
+ | `objGet` | Type-safe property getter |
137
+ | `objSet` | Set a property and return the value |
138
+ | `objHas` | Check if key exists |
139
+ | `objUpdate` | Update a value with a callback |
140
+ | `objGetOrDefault` | Get or create value with factory function |
141
+ | `objGetOrDefaultValue` | Get or set a default value |
142
+ | `objToMap` | Convert object to Map |
143
+ | `objOmitKeysMutable` | Delete multiple keys from an object |
144
+ | `filterObject` | Filter object properties by predicate |
145
+ | `filterObjectMutable` | Filter object in-place |
146
+ | `mapObject` | Map over object values |
147
+ | `mapObjectKeys` | Map over object keys |
148
+ | `mapObjectEntries` | Map over object entries (both key and value) |
149
+ | `defineGetter` | Define a getter property |
150
+ | `defineSetter` | Define a setter property |
151
+ | `defineAccessors` | Define getter and setter |
152
+ | `defineMethod` | Define a method property |
153
+ | `defineValue` | Define a value property |
154
+ | `defineProperty` | Define a property with descriptor |
155
+ | `defineLazyProperty` | Define a lazy-evaluated cached property |
156
+ | `keysOf` | Typed Object.keys |
157
+ | `valuesOf` | Typed Object.values |
158
+ | `entriesOf` | Typed Object.entries |
159
+ | `getPrototypeChain` | Get prototype chain as array |
160
+ | `getClassChain` | Get class constructor chain |
161
+ | `getSuperClass` | Get immediate superclass |
162
+ | `getSuperClasses` | Get all superclasses |
163
+ | `inheritPrototypeMembers` | Copy prototype members between classes |
164
+ | `inheritStaticMembers` | Copy static members between classes |
165
+ | `iterateObject` | Depth-first object traversal generator |
166
+ | `deleteNullishPropsMutable` | Remove null/undefined properties |
167
+ | `className` | Get class name from an instance |
168
+ | `constructorOf` | Get constructor of an object |
169
+ | `hasOwnProperty` | Safe hasOwnProperty check |
170
+ | `hasProperty` | Check own + prototype chain |
171
+ | `getKeys` | Get keys with filtering options |
172
+ | `OptionsConfigurator` | Builder pattern for typed options with defaults |
package/index.d.ts CHANGED
@@ -658,20 +658,15 @@ declare function iterateObject<T extends object = object, V = unknown>(root: T):
658
658
  */
659
659
  declare function keysOf<T extends object>(obj: T): StringKeyOf<T>[];
660
660
 
661
- /**
662
- * Maps over an object's values, transforming each value using the provided function.
663
- */
664
- declare function mapObject<T extends object, V>(obj: T, fn: (value: T[StringKeyOf<T>], key: StringKeyOf<T>) => V): Record<StringKeyOf<T>, V>;
665
-
666
661
  /**
667
662
  * Maps over an object's entries, transforming both keys and values using the provided function.
668
663
  */
669
- declare function mapObjectEntries<T extends object, K extends string, V>(obj: T, fn: (key: StringKeyOf<T>, value: T[StringKeyOf<T>]) => [K, V]): Record<K, V>;
664
+ declare function mapObjectEntries<T extends object, K extends PropertyKey, V>(obj: T, fn: (key: StringKeyOf<T>, value: T[StringKeyOf<T>]) => [K, V]): Record<K, V>;
670
665
 
671
666
  /**
672
667
  * Maps over an object's keys, transforming each key using the provided function while preserving values.
673
668
  */
674
- declare function mapObjectKeys<T extends object, K extends string>(obj: T, fn: (key: StringKeyOf<T>, value: T[StringKeyOf<T>]) => K): Record<K, T[StringKeyOf<T>]>;
669
+ declare function mapObjectKeys<T extends object, K extends PropertyKey>(obj: T, fn: (key: StringKeyOf<T>, value: T[StringKeyOf<T>]) => K): Record<K, T[StringKeyOf<T>]>;
675
670
 
676
671
  /**
677
672
  * Like Object.assign, but only copies source object property values != null.
@@ -748,11 +743,6 @@ declare function objGetOrDefaultValue<T, K extends keyof T>(obj: T, key: K, defa
748
743
  */
749
744
  declare function objHas<V>(obj: Record<PropertyKey, V>, key: PropertyKey): boolean;
750
745
 
751
- /**
752
- * Checks if an object is empty.
753
- */
754
- declare function objIsEmpty<T>(obj: Record<string, T>): boolean;
755
-
756
746
  /**
757
747
  * Deletes the specified keys from an object in a mutable way.
758
748
  * @param obj The object from which to delete the keys.
@@ -898,4 +888,4 @@ type IgnoreKey = (typeof ignoreKeys)[number];
898
888
  */
899
889
  declare function valuesOf<T extends object>(obj: T): T[StringKeyOf<T>][];
900
890
 
901
- export { type AccessorDescriptor, type AccessorsDescriptorAttributes, type DescriptorAccessors, type DescriptorBothAccessors, type DescriptorGetter, type DescriptorSetter, type DescriptorValue, type GetKeysOptions, type IterateObjectYield, type KeysPrimitiveTypeFrom, OptionsConfigurator, type OptsKeyTypeVariants, type OptsKeysVariants, type StaticClassKeyof, type TypedValuePropertyDescriptor, type ValueDescriptor, type ValueDescriptorAttributes, arrAssign, className, classPrototype, constructorOf, createArrayMerger, createObjectMerger, defineAccessors, defineGetter, defineLazyProperty, defineMethod, defineProperty, defineSetter, defineValue, deleteNullishPropsMutable, entriesOf, filterObject, filterObjectMutable, getClassChain, getConfigurableMethodOrGetterKeys, getKeys, getKeysPreset, getOwnProperty, getPrototypeChain, getSuperClass, getSuperClasses, hasOwnProperty, hasProperty, hasPrototypeChainProperty, inheritPrototypeMembers, inheritStaticMembers, isAccessorDescriptor, isEnumerable, isMethodValueDescriptor, isValueDescriptor, iterableFirstElement, iterateObject, keysOf, mapObject, mapObjectEntries, mapObjectKeys, objAssign, objDeepFreeze, objDefineLazyProperty, objDelete, objForEach, objGet, objGetOrDefault, objGetOrDefaultValue, objHas, objIsEmpty, objOmitKeysMutable, objPropertyValueToGetter, objReduce, objSet, objSize, objSortKeys, objToMap, objUpdate, objUpdatePropertyDescriptors, propertyIsEnumerable, setEnumerable, setNonConfigurable, setNonEnumerable, setNonWritable, setWritable, sortKeys, sortKeysLike, staticClassKeysOf, valuesOf };
891
+ export { type AccessorDescriptor, type AccessorsDescriptorAttributes, type DescriptorAccessors, type DescriptorBothAccessors, type DescriptorGetter, type DescriptorSetter, type DescriptorValue, type GetKeysOptions, type IterateObjectYield, type KeysPrimitiveTypeFrom, OptionsConfigurator, type OptsKeyTypeVariants, type OptsKeysVariants, type StaticClassKeyof, type TypedValuePropertyDescriptor, type ValueDescriptor, type ValueDescriptorAttributes, arrAssign, className, classPrototype, constructorOf, createArrayMerger, createObjectMerger, defineAccessors, defineGetter, defineLazyProperty, defineMethod, defineProperty, defineSetter, defineValue, deleteNullishPropsMutable, entriesOf, filterObject, filterObjectMutable, getClassChain, getConfigurableMethodOrGetterKeys, getKeys, getKeysPreset, getOwnProperty, getPrototypeChain, getSuperClass, getSuperClasses, hasOwnProperty, hasProperty, hasPrototypeChainProperty, inheritPrototypeMembers, inheritStaticMembers, isAccessorDescriptor, isEnumerable, isMethodValueDescriptor, isValueDescriptor, iterableFirstElement, iterateObject, keysOf, mapObjectEntries, mapObjectKeys, objAssign, objDeepFreeze, objDefineLazyProperty, objDelete, objForEach, objGet, objGetOrDefault, objGetOrDefaultValue, objHas, objOmitKeysMutable, objPropertyValueToGetter, objReduce, objSet, objSize, objSortKeys, objToMap, objUpdate, objUpdatePropertyDescriptors, propertyIsEnumerable, setEnumerable, setNonConfigurable, setNonEnumerable, setNonWritable, setWritable, sortKeys, sortKeysLike, staticClassKeysOf, valuesOf };
package/index.mjs CHANGED
@@ -370,7 +370,7 @@ __name(isEnumerable, "isEnumerable");
370
370
 
371
371
  // src/getKeysPreset.ts
372
372
  function getKeysPreset(options) {
373
- const ignore = new Set(options?.ignore ?? []);
373
+ const ignore = new Set(options?.ignore);
374
374
  if (options?.ignoreSymbols) {
375
375
  if (options?.ignoreEnumerable) {
376
376
  return (o) => {
@@ -624,7 +624,7 @@ function* iterateObject(root) {
624
624
  propertyPathArray,
625
625
  propertyPath: propertyPathArray.map((s) => {
626
626
  return typeof s === "string" ? s : `[${s}]`;
627
- }).join(".").replace(/\.\[/g, "["),
627
+ }).join(".").replaceAll(".[", "["),
628
628
  isLeaf: false,
629
629
  nodeType: Array.isArray(current) ? "array" : "object"
630
630
  };
@@ -657,7 +657,7 @@ function* iterateObject(root) {
657
657
  propertyPathArray,
658
658
  propertyPath: propertyPathArray.map((s) => {
659
659
  return typeof s === "string" ? s : `[${s}]`;
660
- }).join(".").replace(/\.\[/g, "["),
660
+ }).join(".").replaceAll(".[", "["),
661
661
  isLeaf: true,
662
662
  nodeType: "object"
663
663
  };
@@ -666,16 +666,6 @@ function* iterateObject(root) {
666
666
  }
667
667
  __name(iterateObject, "iterateObject");
668
668
 
669
- // src/mapObject.ts
670
- function mapObject(obj, fn) {
671
- const accum = {};
672
- for (const [key, value] of entriesOf(obj)) {
673
- accum[key] = fn(value, key);
674
- }
675
- return accum;
676
- }
677
- __name(mapObject, "mapObject");
678
-
679
669
  // src/mapObjectEntries.ts
680
670
  function mapObjectEntries(obj, fn) {
681
671
  const accum = {};
@@ -783,15 +773,6 @@ function objHas(obj, key) {
783
773
  }
784
774
  __name(objHas, "objHas");
785
775
 
786
- // src/objIsEmpty.ts
787
- function objIsEmpty(obj) {
788
- for (const _key in obj) {
789
- return false;
790
- }
791
- return true;
792
- }
793
- __name(objIsEmpty, "objIsEmpty");
794
-
795
776
  // src/objOmitKeysMutable.ts
796
777
  function objOmitKeysMutable(obj, ...keys) {
797
778
  for (const key of keys) {
@@ -824,18 +805,18 @@ function objPropertyValueToGetter(object, ...propertyNames) {
824
805
  }
825
806
  __name(objPropertyValueToGetter, "objPropertyValueToGetter");
826
807
 
827
- // ../iter/src/reduceIterable.ts
828
- function reduceIterable(mapLike, reducer, initialValue) {
808
+ // ../iter/src/reduceIterableEntries.ts
809
+ function reduceIterableEntries(mapLike, reducer, initialValue) {
829
810
  for (const [key, value] of mapLike) {
830
811
  initialValue = reducer(initialValue, value, key);
831
812
  }
832
813
  return initialValue;
833
814
  }
834
- __name(reduceIterable, "reduceIterable");
815
+ __name(reduceIterableEntries, "reduceIterableEntries");
835
816
 
836
817
  // src/objReduce.ts
837
818
  function objReduce(object, reducer, accum) {
838
- return reduceIterable(entriesOf(object), reducer, accum);
819
+ return reduceIterableEntries(entriesOf(object), reducer, accum);
839
820
  }
840
821
  __name(objReduce, "objReduce");
841
822
 
@@ -986,4 +967,4 @@ function valuesOf(obj) {
986
967
  }
987
968
  __name(valuesOf, "valuesOf");
988
969
 
989
- export { OptionsConfigurator, arrAssign, className, classPrototype, constructorOf, createArrayMerger, createObjectMerger, defineAccessors, defineGetter, defineLazyProperty, defineMethod, defineProperty, defineSetter, defineValue, deleteNullishPropsMutable, entriesOf, filterObject, filterObjectMutable, getClassChain, getConfigurableMethodOrGetterKeys, getKeys, getKeysPreset, getOwnProperty, getPrototypeChain, getSuperClass, getSuperClasses, hasOwnProperty, hasProperty, hasPrototypeChainProperty, inheritPrototypeMembers, inheritStaticMembers, isAccessorDescriptor, isEnumerable, isMethodValueDescriptor, isValueDescriptor, iterableFirstElement, iterateObject, keysOf, mapObject, mapObjectEntries, mapObjectKeys, objAssign, objDeepFreeze, objDefineLazyProperty, objDelete, objForEach, objGet, objGetOrDefault, objGetOrDefaultValue, objHas, objIsEmpty, objOmitKeysMutable, objPropertyValueToGetter, objReduce, objSet, objSize, objSortKeys, objToMap, objUpdate, objUpdatePropertyDescriptors, propertyIsEnumerable, setEnumerable, setNonConfigurable, setNonEnumerable, setNonWritable, setWritable, sortKeys, sortKeysLike, staticClassKeysOf, valuesOf };
970
+ export { OptionsConfigurator, arrAssign, className, classPrototype, constructorOf, createArrayMerger, createObjectMerger, defineAccessors, defineGetter, defineLazyProperty, defineMethod, defineProperty, defineSetter, defineValue, deleteNullishPropsMutable, entriesOf, filterObject, filterObjectMutable, getClassChain, getConfigurableMethodOrGetterKeys, getKeys, getKeysPreset, getOwnProperty, getPrototypeChain, getSuperClass, getSuperClasses, hasOwnProperty, hasProperty, hasPrototypeChainProperty, inheritPrototypeMembers, inheritStaticMembers, isAccessorDescriptor, isEnumerable, isMethodValueDescriptor, isValueDescriptor, iterableFirstElement, iterateObject, keysOf, mapObjectEntries, mapObjectKeys, objAssign, objDeepFreeze, objDefineLazyProperty, objDelete, objForEach, objGet, objGetOrDefault, objGetOrDefaultValue, objHas, objOmitKeysMutable, objPropertyValueToGetter, objReduce, objSet, objSize, objSortKeys, objToMap, objUpdate, objUpdatePropertyDescriptors, propertyIsEnumerable, setEnumerable, setNonConfigurable, setNonEnumerable, setNonWritable, setWritable, sortKeys, sortKeysLike, staticClassKeysOf, valuesOf };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/object",
3
- "version": "1.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "object",
5
5
  "type": "module",
6
6
  "sideEffects": false,