Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7aca9ce

Browse files
committed
docs: update API documentation
1 parent 9376818 commit 7aca9ce

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

README.md

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ At this step, we need to create our `mapping` against our data `source`.
7272

7373
We will be using `dot notation` to create our `final structure`.
7474

75-
> For more info about `dot notation` API, check out the [documentation](https://github.com/arg-def/mapper-js)
75+
> For more info about `dot notation` API, check out the [documentation](https://github.com/the-cookbook/dot-notation)
7676
7777
With `mapper`, it is possible to `get` _one_ or _several_ values from our `source`
7878
and even `transform` it in the way we need.
@@ -89,11 +89,11 @@ the selected values as array in the `parameter`.
8989
Now let's create our `mapping`!
9090

9191
```js
92-
import mapper from '@arg-def/mapper-js';
92+
import mapper from '@cookbook/mapper-js';
9393

9494
...
9595

96-
const mapping = mapper.mapping((map) => ({
96+
const mapping = mapper((map) => ({
9797
'person.name': map('person.name')
9898
.transform(({ firstName, lastName }) => `${firstName} ${lastName}`)
9999
.value,
@@ -110,10 +110,10 @@ const mapping = mapper.mapping((map) => ({
110110
### 3) Create your mapped object
111111

112112
```js
113-
import mapper from '@arg-def/mapper-js';
113+
import mapper from '@cookbook/mapper-js';
114114
...
115115

116-
const result = mapper(source, mapping);
116+
const result = mapping(source);
117117
/* outputs
118118
{
119119
person: {
@@ -147,18 +147,42 @@ const result = mapper(source, mapping);
147147
## mapper
148148

149149
**Type:** `function()`
150-
**Parameter:** `source: object, mapping: IMapping, options?: IMapperOptions`
150+
**Parameter:** `mapping: Mapping`
151+
**Return:** `<T>(source: object | object[], options?: Options) => T extends [] ? T[] : T`,
152+
**Signature:** `(mapping: Mapping) => <T>(source: object | object[], options?: Options) => T extends [] ? T[] : T`
151153

152-
**Description:**
153154

154-
`mapper()` mappes your _source data_ against your _mapping_.
155+
**Description:**
156+
157+
`mapper()` is the main method and responsible for mapping the _values_ from your _data source_ against the _mapping instructions_.
158+
It accepts `dot notation` path(s) as `key(s)`.
159+
160+
Example:
161+
162+
```ts
163+
// raw definition
164+
const mapping = mapper((map) => ({
165+
...
166+
}));
167+
168+
// with map() query
169+
const mapping = mapper((map) => ({
170+
'employee.name': map('person.name.firstName').value,
171+
'employee.age': map('person.name.age').value,
172+
'employee.address': map('person.address').value,
173+
}));
174+
```
175+
176+
As a result from the above implementation, `mapper()` return a new `function` to map and compile your _source data_ against your _mapping_.
155177

156178
It accepts an extra (_optional_) argument defining the [_global mapping options_](#mapper-options).
157179

158180
Example:
159181

160182
```ts
161-
mapper(source, mapping, options);
183+
...
184+
185+
mapping(source, options);
162186

163187
/* outputs
164188
{
@@ -181,41 +205,13 @@ mapper(source, mapping, options);
181205
```
182206
___
183207

184-
## mapper.mapping
185-
186-
**Type**: `function()`
187-
**Parameter**: `map`
188-
**Signature:** `(callback: IMapping): IMapping => callback;`
189-
190-
**Description:**
191-
192-
`mapper.mapping()` is the method responsible for mapping the _values_ from your _source data_ against your _object shape_.
193-
It accepts `dot notation` path as `key`.
194-
195-
Example:
196-
```ts
197-
// raw definition
198-
const mapping = mapper.mapping((map) => ({
199-
...
200-
}));
201-
202-
// with map() query
203-
const mapping = mapper.mapping((map) => ({
204-
'employee.name': map('person.name.firstName').value,
205-
'employee.age': map('person.name.age').value,
206-
'employee.address': map('person.address').value,
207-
}));
208-
209-
210-
```
211-
___
212-
213208

214209
## map
215210

216211
**Type:** `function`
217-
**Parameter:** `paths: string|string[], option?: IMapperOptions`
218-
**Signature:** `<T>(key: string | string[], options?: IMapperOptions) => IMapMethods<T>;`
212+
**Parameter:** `keys: string | string[], options?: Options`
213+
**Return:** `MapMethods<T>`,
214+
**Signature:** `<T = unknown>(keys: string | string[], options?: Options) => MapMethods<T>`
219215

220216
**Description:**
221217

@@ -235,7 +231,8 @@ map(['person.name.firstName', 'person.name.lastName'], options);
235231

236232
**Type:** `function`
237233
**Parameter:** `...unknown[]`
238-
**Signature:** `(callback: (...args: unknown[]) => T) => IMapMethods<T>`
234+
**Return:** `unknown | unknown[]`,
235+
**Signature:** `(...args: unknown[]) => unknown | unknown[]`
239236

240237
**Description:**
241238

@@ -251,14 +248,14 @@ map('person.name.firstName')
251248

252249
// multiple values
253250
map(['person.name.firstName', 'person.name.lastName'])
254-
.transform((firstName, lastName) => `${firstName} ${lastName]`);
251+
.transform((firstName, lastName) => `${firstName} ${lastName}`);
255252
```
256253

257254

258255
#### `value`
259256

260257
**Type:** `readonly`
261-
**Returns:** `T`
258+
**Return:** `T`
262259
**Description:**
263260

264261
`.value` returns the value of your `dot notation` query. If transformed, returns the transformed value.
@@ -272,7 +269,7 @@ map('person.name.firstName')
272269

273270
// multiple values
274271
map(['person.name.firstName', 'person.name.lastName'])
275-
.transform((firstName, lastName) => `${firstName} ${lastName]`)
272+
.transform((firstName, lastName) => `${firstName} ${lastName}`)
276273
.value;
277274
```
278275

@@ -283,14 +280,14 @@ map(['person.name.firstName', 'person.name.lastName'])
283280

284281
```js
285282
{
286-
suppressNullUndefined: false,
287-
suppressionStrategy: () => false,
283+
omitNullUndefined: false,
284+
omitStrategy: () => false,
288285
}
289286
```
290287

291288
### Details
292289

293-
**`suppressNullUndefined`**
290+
**`omitNullUndefined`**
294291

295292
**Type:** `boolean`
296293
**default value:** `false`
@@ -300,6 +297,7 @@ map(['person.name.firstName', 'person.name.lastName'])
300297
Removes `null` or `undefined` entries from the _mapped_ object.
301298

302299
Example:
300+
303301
```ts
304302
/* source object
305303
{
@@ -310,15 +308,15 @@ Example:
310308
},
311309
}
312310
*/
313-
const mapping = mapper.mapping((map) => ({
311+
const mapping = mapper((map) => ({
314312
'name': map('person.name').value,
315313
'age': map('person.age').value,
316314
// source doesn't have property 'address',
317315
// therefore will return "undefined"
318316
'address': map('person.address').value,
319317
}));
320318

321-
mapper(source, mapping, { suppressNullUndefined: true });
319+
mapping(source, { omitNullUndefined: true });
322320
/* outputs
323321
{
324322
name: 'John',
@@ -328,16 +326,16 @@ mapper(source, mapping, { suppressNullUndefined: true });
328326

329327
```
330328

331-
332-
**`suppressionStrategy`**
329+
**`omitStrategy`**
333330

334331
**Type:** `function`
335-
**Parameter:** `value: unknown`
336-
**Signature:** `(value: unknown) => boolean`
332+
**Parameter:** `value: unknown | unknown[]`
333+
**Return:** `boolean`
334+
**Signature:** `(value: unknown | unknown[]) => boolean`
337335

338336
**Description:**
339337

340-
Defines a _custom strategy_ to suppress entries from the _mapped object_.
338+
Defines a _custom strategy_ to omit (_suppress_) entries from the _mapped object_.
341339

342340
Example:
343341
```tsx
@@ -358,15 +356,15 @@ Example:
358356
}
359357
*/
360358

361-
const customSuppressionStrategy = (address: ISource): boolean => address && address.city === 'Cupertino';
359+
const customOmitStrategy = (address: Record<string, string>): boolean => address && address.city === 'Cupertino';
362360

363-
const mapping = mapper.mapping((map) => ({
361+
const mapping = mapper((map) => ({
364362
'name': map('person.name').value,
365363
'age': map('person.age').value,
366364
'address': map('person.address').value,
367365
}));
368366

369-
mapper(source, mapping, { suppressionStrategy: customSuppressionStrategy );
367+
mapping(source, { omitStrategy: customOmitStrategy });
370368
/* outputs
371369
{
372370
name: 'John',

0 commit comments

Comments
 (0)