|
| 1 | +[js-stdlib-extensions](/docs/README.md) / [Exports](/docs/modules.md) / [ObjectExtensions](/docs/modules/ObjectExtensions.md) / ObjectConstructor |
| 2 | + |
| 3 | +# Interface: ObjectConstructor |
| 4 | + |
| 5 | +[ObjectExtensions](/docs/modules/ObjectExtensions.md).ObjectConstructor |
| 6 | + |
| 7 | +ObjectConstructor |
| 8 | + |
| 9 | +## Table of contents |
| 10 | + |
| 11 | +### Methods |
| 12 | + |
| 13 | +- [flatMap](/docs/interfaces/ObjectExtensions.ObjectConstructor.md#flatmap) |
| 14 | +- [isEmpty](/docs/interfaces/ObjectExtensions.ObjectConstructor.md#isempty) |
| 15 | +- [sort](/docs/interfaces/ObjectExtensions.ObjectConstructor.md#sort) |
| 16 | + |
| 17 | +## Methods |
| 18 | + |
| 19 | +### flatMap |
| 20 | + |
| 21 | +▸ **flatMap**\<`T`\>(`o`, `options?`): \{ `[key: string]`: `string` \| [value: string, type: string]; }[] |
| 22 | + |
| 23 | +Returns a flat map of a multi-dimensional object with, or without, type |
| 24 | +definitions of the value. |
| 25 | + |
| 26 | +#### Type parameters |
| 27 | + |
| 28 | +| Name | |
| 29 | +| :------ | |
| 30 | +| `T` | |
| 31 | + |
| 32 | +#### Parameters |
| 33 | + |
| 34 | +| Name | Type | Description | |
| 35 | +| :------ | :------ | :------ | |
| 36 | +| `o` | `T` | Object. | |
| 37 | +| `options?` | `Object` | - | |
| 38 | +| `options.types?` | `boolean` | If false, will return only the value with each object key, instead of an array that contains the value with the type definition of the value. | |
| 39 | + |
| 40 | +#### Returns |
| 41 | + |
| 42 | +\{ `[key: string]`: `string` \| [value: string, type: string]; }[] |
| 43 | + |
| 44 | +**`Example`** |
| 45 | + |
| 46 | +```ts |
| 47 | +// returns [ |
| 48 | +// { name: [ 'John', 'string' ] }, |
| 49 | +// { age: [ 42, 'number' ] }, |
| 50 | +// { 'children.0.name': [ 'Mary', 'string' ] }, |
| 51 | +// { 'children.0.age': [ 18, 'number' ] }, |
| 52 | +// { 'children.1.name': [ 'Nicholas', 'string' ] }, |
| 53 | +// { 'children.1.age': [ 11, 'number' ] }, |
| 54 | +// { 'children.2.name': [ 'Adam', 'string' ] }, |
| 55 | +// { 'children.2.age': [ 21, 'number' ] }, |
| 56 | +// { 'letters.0': [ 'a', 'string' ] }, |
| 57 | +// { 'letters.1': [ 'd', 'string' ] }, |
| 58 | +// { 'letters.2': [ 'z', 'string' ] }, |
| 59 | +// { 'letters.3': [ 'c', 'string' ] }, |
| 60 | +// { 'numbers.0': [ 9, 'number' ] }, |
| 61 | +// { 'numbers.1': [ 1, 'number' ] }, |
| 62 | +// { 'numbers.3': [ 4, 'number' ] } |
| 63 | +// ] |
| 64 | +Object.flatMap({ |
| 65 | + name: 'John', |
| 66 | + age: 42, |
| 67 | + single: false, |
| 68 | + children: [ |
| 69 | + { name: 'Mary', age: 18 }, |
| 70 | + { name: 'Nicholas', age: 11 }, |
| 71 | + { name: 'Adam', age: 21 } |
| 72 | + ], |
| 73 | + letters: [ 'a', 'd', 'z', 'c' ], |
| 74 | + numbers: [ 9, 1, 0, 4 ] |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +**`Example`** |
| 79 | + |
| 80 | +```ts |
| 81 | +// returns [ |
| 82 | +// { name: 'John' }, |
| 83 | +// { age: 42 }, |
| 84 | +// { 'children.0.name': 'Mary' }, |
| 85 | +// { 'children.0.age': 18 }, |
| 86 | +// { 'children.1.name': 'Nicholas' }, |
| 87 | +// { 'children.1.age': 11 }, |
| 88 | +// { 'children.2.name': 'Adam' }, |
| 89 | +// { 'children.2.age': 21 }, |
| 90 | +// { 'letters.0': 'a' }, |
| 91 | +// { 'letters.1': 'd' }, |
| 92 | +// { 'letters.2': 'z' }, |
| 93 | +// { 'letters.3': 'c' }, |
| 94 | +// { 'numbers.0': 9 }, |
| 95 | +// { 'numbers.1': 1 }, |
| 96 | +// { 'numbers.3': 4 } |
| 97 | +// ] |
| 98 | +Object.flatMap({ |
| 99 | + name: 'John', |
| 100 | + age: 42, |
| 101 | + single: false, |
| 102 | + children: [ |
| 103 | + { name: 'Mary', age: 18 }, |
| 104 | + { name: 'Nicholas', age: 11 }, |
| 105 | + { name: 'Adam', age: 21 } |
| 106 | + ], |
| 107 | + letters: [ 'a', 'd', 'z', 'c' ], |
| 108 | + numbers: [ 9, 1, 0, 4 ] |
| 109 | +}, {types: false}) |
| 110 | +``` |
| 111 | + |
| 112 | +#### Defined in |
| 113 | + |
| 114 | +[object.ts:82](https://github.com/KamaranL/js-stdlib-extensions/blob/c125d3e/src/ext/object.ts#L82) |
| 115 | + |
| 116 | +___ |
| 117 | + |
| 118 | +### isEmpty |
| 119 | + |
| 120 | +▸ **isEmpty**\<`T`\>(`o`): `boolean` |
| 121 | + |
| 122 | +Return true if the length of Object.keys() is 0. |
| 123 | + |
| 124 | +#### Type parameters |
| 125 | + |
| 126 | +| Name | |
| 127 | +| :------ | |
| 128 | +| `T` | |
| 129 | + |
| 130 | +#### Parameters |
| 131 | + |
| 132 | +| Name | Type | Description | |
| 133 | +| :------ | :------ | :------ | |
| 134 | +| `o` | `T` | Object. | |
| 135 | + |
| 136 | +#### Returns |
| 137 | + |
| 138 | +`boolean` |
| 139 | + |
| 140 | +**`Example`** |
| 141 | + |
| 142 | +```ts |
| 143 | +// returns true |
| 144 | +Object.isEmpty({}) |
| 145 | +``` |
| 146 | + |
| 147 | +#### Defined in |
| 148 | + |
| 149 | +[object.ts:99](https://github.com/KamaranL/js-stdlib-extensions/blob/c125d3e/src/ext/object.ts#L99) |
| 150 | + |
| 151 | +___ |
| 152 | + |
| 153 | +### sort |
| 154 | + |
| 155 | +▸ **sort**\<`T`\>(`o`, `options?`): `T` |
| 156 | + |
| 157 | +Returns an object with sorted keys. |
| 158 | + |
| 159 | +#### Type parameters |
| 160 | + |
| 161 | +| Name | |
| 162 | +| :------ | |
| 163 | +| `T` | |
| 164 | + |
| 165 | +#### Parameters |
| 166 | + |
| 167 | +| Name | Type | Description | |
| 168 | +| :------ | :------ | :------ | |
| 169 | +| `o` | `T` | Object. | |
| 170 | +| `options?` | `Object` | - | |
| 171 | +| `options.descending?` | `boolean` | If true, will sort object keys in descending order. | |
| 172 | +| `options.property?` | `string` | If provided, will sort arrays of objects by the specified property. | |
| 173 | +| `options.recursive?` | `boolean` | If true, will recursively sort any nested objects/arrays. | |
| 174 | + |
| 175 | +#### Returns |
| 176 | + |
| 177 | +`T` |
| 178 | + |
| 179 | +**`Example`** |
| 180 | + |
| 181 | +```ts |
| 182 | +// returns { |
| 183 | +// age: 42, |
| 184 | +// children: [ |
| 185 | +// { name: 'Mary', age: 18 }, |
| 186 | +// { name: 'Nicholas', age: 11 }, |
| 187 | +// { name: 'Adam', age: 21 } |
| 188 | +// ], |
| 189 | +// letters: [ 'a', 'd', 'z', 'c' ], |
| 190 | +// name: 'John', |
| 191 | +// numbers: [ 9, 1, 0, 4 ], |
| 192 | +// single: false |
| 193 | +// } |
| 194 | +Object.sort({ |
| 195 | + name: 'John', |
| 196 | + age: 42, |
| 197 | + single: false, |
| 198 | + children: [ |
| 199 | + { name: 'Mary', age: 18 }, |
| 200 | + { name: 'Nicholas', age: 11 }, |
| 201 | + { name: 'Adam', age: 21 } |
| 202 | + ], |
| 203 | + letters: [ 'a', 'd', 'z', 'c' ], |
| 204 | + numbers: [ 9, 1, 0, 4 ] |
| 205 | +}) |
| 206 | +``` |
| 207 | + |
| 208 | +**`Example`** |
| 209 | + |
| 210 | +```ts |
| 211 | +// returns { |
| 212 | +// age: 42, |
| 213 | +// children: [ |
| 214 | +// { age: 11, name: 'Nicholas' }, |
| 215 | +// { age: 18, name: 'Mary' }, |
| 216 | +// { age: 21, name: 'Adam' } |
| 217 | +// ], |
| 218 | +// letters: [ 'a', 'c', 'd', 'z' ], |
| 219 | +// name: 'John', |
| 220 | +// numbers: [ 0, 1, 4, 9 ], |
| 221 | +// single: false |
| 222 | +// } |
| 223 | +Object.sort({ |
| 224 | + name: 'John', |
| 225 | + age: 42, |
| 226 | + single: false, |
| 227 | + children: [ |
| 228 | + { name: 'Mary', age: 18 }, |
| 229 | + { name: 'Nicholas', age: 11 }, |
| 230 | + { name: 'Adam', age: 21 } |
| 231 | + ], |
| 232 | + letters: [ 'a', 'd', 'z', 'c' ], |
| 233 | + numbers: [ 9, 1, 0, 4 ] |
| 234 | +}, {recursive: true, property: 'age'}) |
| 235 | +``` |
| 236 | + |
| 237 | +#### Defined in |
| 238 | + |
| 239 | +[object.ts:168](https://github.com/KamaranL/js-stdlib-extensions/blob/c125d3e/src/ext/object.ts#L168) |
0 commit comments