|
2 | 2 | > A class that maps between a variety of data types. |
3 | 3 |
|
4 | 4 | ## Installation |
5 | | -Simply do: `npm install marshaller`. |
| 5 | +Simply do: `npm install @wessberg/marshaller`. |
6 | 6 |
|
7 | 7 | ## Usage |
8 | | -```typescript |
| 8 | +```javascript |
9 | 9 | const marshaller = new Marshaller(); |
10 | 10 |
|
11 | 11 | marshaller.marshal("true", Boolean); // true |
12 | 12 | marshaller.marshal("0", Boolean); // false |
13 | 13 | marshaller.marshal([1, 2, false, true], String); // [ 1, 2, false, true] |
14 | 14 | marshaller.marshal("Infinity"); // Auto-mapped to number - Infinity. |
| 15 | +marshaller.marshal<number, string>(123, "a hint") // "123", generic typecasting. |
15 | 16 | ``` |
16 | 17 |
|
17 | 18 | As you can see, you simply pass some arbitrary data and *optionally* the type |
18 | 19 | you want to map to. If you don't provide any, `Marshaller` will attempt to find |
19 | 20 | the most appropriate type to map to using heuristics. The second argument, the hint, |
20 | 21 | accepts either a constructor for a type or a concrete instance of one. |
21 | 22 |
|
| 23 | +## API |
| 24 | +`marshal<T, U> (data: T, hint?: U|Newable<U>): U | null|undefined` |
| 25 | + |
| 26 | +#### Params |
| 27 | +##### `data: T` |
| 28 | +The input data. |
| 29 | + |
| 30 | +##### `hint?: U|Newable<U>` |
| 31 | +An optional hint to define which data type to marshal to. If omitted, the data will be parsed |
| 32 | +into the type `Marshaller` finds most appropriate. |
| 33 | + |
| 34 | +#### Returns |
| 35 | +##### `U` |
| 36 | +The marshalled version of the input data. |
| 37 | + |
| 38 | +## Roadmap |
| 39 | +* [X] Casting from/to `Set`. |
| 40 | +* [X] Casting from/to `Object`. |
| 41 | +* [X] Casting from/to `String`. |
| 42 | +* [X] Casting from/to `Number`. |
| 43 | +* [X] Casting from/to `Boolean`. |
| 44 | +* [X] Casting from/to `Array`. |
| 45 | +* [ ] Casting from/to `Date`. |
| 46 | +* [ ] Casting from/to `Function` |
| 47 | +* [ ] Casting from/to `Map` |
| 48 | +* [ ] Casting from/to `WeakSet` |
| 49 | +* [ ] Casting from/to `WeakMap` |
| 50 | +* [ ] Casting from/to `RegExp` |
| 51 | + |
22 | 52 | ## Changelog: |
23 | 53 |
|
24 | | -**v1.0**: |
| 54 | +**v1.0.2**: |
| 55 | + |
| 56 | +- Updated README and a few typings. |
| 57 | + |
| 58 | +**v1.0.0**: |
25 | 59 |
|
26 | 60 | - First release. |
27 | 61 |
|
28 | | -[npm-url]: https://npmjs.org/package/marshaller |
29 | | -[npm-image]: https://badge.fury.io/js/marshaller.svg |
| 62 | +[npm-url]: https://npmjs.org/package/@wessberg/marshaller |
| 63 | +[npm-image]: https://badge.fury.io/js/@wessberg/marshaller.svg |
0 commit comments