|
1 |
| -import { NamespacePlugin, Element } from 'minim'; |
2 |
| -import { isPlainObject } from 'ramda-adjunct'; |
| 1 | +import { has } from 'ramda'; |
| 2 | +import { isPlainObject, isString } from 'ramda-adjunct'; |
| 3 | +import { NamespacePlugin, Element, Namespace as INamespace } from 'minim'; |
3 | 4 |
|
4 | 5 | import { Namespace as ApiDOMNamespace } from './namespace';
|
5 | 6 |
|
@@ -56,19 +57,44 @@ export const createNamespace = (namespacePlugin?: NamespacePlugin): ApiDOMNamesp
|
56 | 57 | return namespace;
|
57 | 58 | };
|
58 | 59 |
|
59 |
| -export const toJSON = (namespace: ApiDOMNamespace, element: Element): JSON => |
60 |
| - namespace.toRefract(element); |
61 |
| - |
62 |
| -export const toJSONString = (namespace: ApiDOMNamespace, element: Element): string => |
63 |
| - JSON.stringify(toJSON(namespace, element)); |
| 60 | +/** |
| 61 | + * Transforms data to an Element from a particular namespace. |
| 62 | + */ |
| 63 | +export const from = (data: any, namespace: INamespace): Element => { |
| 64 | + if (isString(data)) { |
| 65 | + // JSON serialized refract |
| 66 | + return namespace.fromRefract(JSON.parse(data)); |
| 67 | + } |
| 68 | + if (isPlainObject(data) && has('element', data)) { |
| 69 | + // refract javascript structure |
| 70 | + return namespace.fromRefract(data); |
| 71 | + } |
| 72 | + if (isPlainObject(data)) { |
| 73 | + // javascript POJO |
| 74 | + return namespace.toElement(data); |
| 75 | + } |
| 76 | + throw new Error('Data was not recognized'); |
| 77 | +}; |
64 | 78 |
|
65 |
| -export const fromJSON = (namespace: ApiDOMNamespace, json: JSON): Element => |
66 |
| - namespace.fromRefract(json); |
| 79 | +/** |
| 80 | + * Reconstructs the ApiDOM into JavaScript POJO. |
| 81 | + * This POJO would be the result of parsing the original |
| 82 | + * JSON string with JSON.parse function. |
| 83 | + */ |
| 84 | +export const toValue = (element: Element): any => element.toValue(); |
67 | 85 |
|
68 |
| -export const fromJSONString = (namespace: ApiDOMNamespace, jsonString: string): Element => |
69 |
| - fromJSON(namespace, JSON.parse(jsonString)); |
| 86 | +/** |
| 87 | + * Create a refract representation of Element. |
| 88 | + * https://github.com/refractproject/refract-spec |
| 89 | + */ |
| 90 | +export const dehydrate = (element: Element, namespace: INamespace): Record<string, any> => { |
| 91 | + return namespace.toRefract(element); |
| 92 | +}; |
70 | 93 |
|
71 |
| -// Reconstructs the ApiDOM into JavaScript POJO. |
72 |
| -// This POJO would be the result of parsing the original |
73 |
| -// JSON string with JSON.parse function. |
74 |
| -export const toValue = (element: Element): any => element.toValue(); |
| 94 | +/** |
| 95 | + * Create a string representation of Element. |
| 96 | + */ |
| 97 | +export const toString = (element: Element, namespace: INamespace): string => { |
| 98 | + const refract = dehydrate(element, namespace); |
| 99 | + return JSON.stringify(refract); |
| 100 | +}; |
0 commit comments