|
| 1 | +/* eslint-disable no-bitwise, no-underscore-dangle */ |
| 2 | + |
| 3 | +const setFlag = (mask, options) => (options | mask); |
| 4 | +const isFlag = (mask, options) => (options & mask) !== 0; |
| 5 | + |
| 6 | +const FIXED_FLAG = 1 << 0; |
| 7 | +const NULLABLE_FLAG = 1 << 1; |
| 8 | + |
| 9 | +function findDefault(e) { |
| 10 | + if (undefined !== e._attributes) { |
| 11 | + const result = e.attributes.get('default'); |
| 12 | + if (undefined !== result) { |
| 13 | + return result; |
| 14 | + } |
| 15 | + } |
| 16 | + return null; |
| 17 | +} |
| 18 | + |
| 19 | +function hasTypeAttribute(e, attribute) { |
| 20 | + if (undefined !== e._attributes) { |
| 21 | + const attrs = e.attributes.get('typeAttributes'); |
| 22 | + if (undefined !== attrs && undefined !== attrs.content) { |
| 23 | + return undefined !== attrs.content.find(attr => attr.content === attribute); |
| 24 | + } |
| 25 | + } |
| 26 | + return false; |
| 27 | +} |
| 28 | + |
| 29 | +const hasFixedTypeAttribute = e => hasTypeAttribute(e, 'fixed'); |
| 30 | +const hasNullableTypeAttribute = e => hasTypeAttribute(e, 'nullable'); |
| 31 | + |
| 32 | +function updateTypeAttributes(e, options) { |
| 33 | + let result = options; |
| 34 | + if (hasFixedTypeAttribute(e)) { |
| 35 | + result = setFlag(FIXED_FLAG, result); |
| 36 | + } |
| 37 | + if (hasNullableTypeAttribute(e)) { |
| 38 | + result = setFlag(NULLABLE_FLAG, result); |
| 39 | + } |
| 40 | + return result; |
| 41 | +} |
| 42 | + |
| 43 | +module.exports = (namespace) => { |
| 44 | + const { Element } = namespace.elements; |
| 45 | + const { |
| 46 | + Array: ArrayElement, |
| 47 | + String: StringElement, |
| 48 | + Boolean: BooleanElement, |
| 49 | + Number: NumberElement, |
| 50 | + Null: NullElement, |
| 51 | + } = namespace.elements; |
| 52 | + |
| 53 | + function findFirstSample(e) { |
| 54 | + if (undefined !== e._attributes) { |
| 55 | + const samples = e.attributes.get('samples'); |
| 56 | + if (samples instanceof ArrayElement) { |
| 57 | + if (undefined !== samples.content && samples.content.length > 0) { |
| 58 | + return samples.content[0]; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + const isPrimitive = e => (e instanceof StringElement) || (e instanceof NumberElement) || (e instanceof BooleanElement); |
| 66 | + |
| 67 | + // Generate default value for leaf elements (Boolean, String, Number, Null) |
| 68 | + function generateValue(e) { |
| 69 | + if (e instanceof BooleanElement) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + if (e instanceof NumberElement) { |
| 73 | + return 0; |
| 74 | + } |
| 75 | + if (e instanceof StringElement) { |
| 76 | + return ''; |
| 77 | + } |
| 78 | + if (e instanceof NullElement) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + |
| 84 | + // Create instance of leaf element (Boolean, String, Number, Null) |
| 85 | + function instantiateLeaf(e, options) { |
| 86 | + const opts = updateTypeAttributes(e, options); |
| 87 | + if (e.content) { |
| 88 | + return [e.content, 'content']; |
| 89 | + } |
| 90 | + const sample = findFirstSample(e); |
| 91 | + if (sample) { |
| 92 | + return [sample.content, 'sample']; |
| 93 | + } |
| 94 | + const dflt = findDefault(e); |
| 95 | + if (dflt) { |
| 96 | + return [dflt.content, 'default']; |
| 97 | + } |
| 98 | + if (isFlag(NULLABLE_FLAG, opts)) { |
| 99 | + return [null, 'content']; |
| 100 | + } |
| 101 | + |
| 102 | + return [generateValue(e), 'generated']; |
| 103 | + } |
| 104 | + |
| 105 | + function instantiate(e, options) { |
| 106 | + if (isPrimitive(e) === true) { |
| 107 | + return instantiateLeaf(e, options); |
| 108 | + } |
| 109 | + if (e instanceof NullElement) { |
| 110 | + return instantiateLeaf(e, options); |
| 111 | + } |
| 112 | + return undefined; |
| 113 | + } |
| 114 | + |
| 115 | + if (!Object.getOwnPropertyNames(Element.prototype).includes('instantiate')) { |
| 116 | + Object.defineProperty(Element.prototype, 'instantiate', { |
| 117 | + value() { return instantiate(this, 0); }, |
| 118 | + }); |
| 119 | + } |
| 120 | +}; |
0 commit comments