|
| 1 | +import _ from 'lodash'; |
| 2 | +// @ts-expect-error no types exist for this library |
| 3 | +import { flatten, unflatten } from 'flat'; |
| 4 | +import { getTypeDescriptorForValue } from './bson-csv'; |
| 5 | +/** |
| 6 | + * TODO: lucas: Some overlap w/ bson-csv but they do |
| 7 | + * have difference! Can't quite name it yet, but something |
| 8 | + * to sort in the future. |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Converts any nested objects into a single depth object with `dotnotation` keys. |
| 13 | + * @example |
| 14 | + * ```javascript |
| 15 | + * dotnotation.serialize({_id: 'arlo', collar: {size: 14}}); |
| 16 | + * >> {_id: 'arlo', 'collar.size': 14} |
| 17 | +
|
| 18 | + * dotnotation.serialize({ foo: { 1: 'one', two: 'two' } }); |
| 19 | + * >> { 'foo.1': 'one', 'foo.two': 'two' } |
| 20 | +
|
| 21 | + * dotnotation.serialize({ foo: { 1: 'one', two: 'two' } }, { includeObjects: true }); |
| 22 | + * >> { foo: {}, 'foo.1': 'one', 'foo.two': 'two' } |
| 23 | + * ``` |
| 24 | + * @param {Object} obj |
| 25 | + * @returns {Object} |
| 26 | + */ |
| 27 | +export function serialize( |
| 28 | + obj: Record<string, unknown>, |
| 29 | + { includeObjects = false } = {} |
| 30 | +): Record<string, unknown> { |
| 31 | + const flattened = flatten(obj, { |
| 32 | + safe: true, // preserve arrays and their contents |
| 33 | + /** |
| 34 | + * @param {any} value |
| 35 | + * @returns {Boolean} |
| 36 | + * NOTE: lucas: Trying an existing fork that supports this new option: |
| 37 | + * https://github.com/hughsk/flat/pull/93 |
| 38 | + */ |
| 39 | + ignoreValue: function (value: unknown): boolean { |
| 40 | + const t = getTypeDescriptorForValue(value); |
| 41 | + if (t.isBSON) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + return false; |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + if (includeObjects) { |
| 49 | + /* |
| 50 | + Make sure that paths to objects exist in the returned value before the paths |
| 51 | + to properties inside those objects. |
| 52 | + ie. for { foo: { 1: 'one', two: 'two' } } we will return |
| 53 | + { foo: {}, 'foo.1': 'one', 'foo.two': 'two' } rather than |
| 54 | + { 'foo.1': 'one', 'foo.two': 'two'}. |
| 55 | +
|
| 56 | + This way when we walk the return value later by the time we encounter |
| 57 | + 'foo.1' we already created foo, initialised to {}. Then _.set(result, |
| 58 | + 'foo.1', 'one') will not create foo as an array because 1 looks like an |
| 59 | + index. This is because at that point result will already contain { foo: {} } |
| 60 | +
|
| 61 | + The use-case for this came about because paths that end with numbers are |
| 62 | + ambiguous and _.set() will assume it is an array index by default. By |
| 63 | + ensuring that there is already an object at the target the ambiguity is |
| 64 | + removed. |
| 65 | + */ |
| 66 | + const withObjects: Record<string, unknown> = {}; |
| 67 | + const knownParents: Record<string, true> = {}; |
| 68 | + for (const [path, value] of Object.entries(flattened)) { |
| 69 | + const parentPath = path.includes('.') |
| 70 | + ? path.slice(0, path.lastIndexOf('.')) |
| 71 | + : null; |
| 72 | + if (parentPath && !knownParents[parentPath]) { |
| 73 | + knownParents[parentPath] = true; |
| 74 | + // Leave arrays alone because they already got handled by safe: true above. |
| 75 | + if (!Array.isArray(_.get(obj, parentPath))) { |
| 76 | + withObjects[parentPath] = {}; |
| 77 | + } |
| 78 | + } |
| 79 | + withObjects[path] = value; |
| 80 | + } |
| 81 | + return withObjects; |
| 82 | + } |
| 83 | + |
| 84 | + return flattened; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Converts an object using dotnotation to a full, nested object. |
| 89 | + * @example |
| 90 | + * ```javascript |
| 91 | + * dotnotation.deserialize({_id: 'arlo', 'collar.size': 14}); |
| 92 | + * >> {_id: 'arlo', collar: {size: 14}} |
| 93 | + * ``` |
| 94 | + * @param {Object} obj |
| 95 | + * @returns {Object} |
| 96 | + */ |
| 97 | +export function deserialize(obj: any): any { |
| 98 | + /** |
| 99 | + * TODO: lucas: bson type support. For now, drop. |
| 100 | + */ |
| 101 | + return unflatten(obj); |
| 102 | +} |
| 103 | + |
| 104 | +export default { serialize, deserialize }; |
0 commit comments