[d24f17c] | 1 | const negate = require('lodash/negate');
|
---|
| 2 | const isObject = require('lodash/isObject');
|
---|
| 3 |
|
---|
| 4 | const ArrayElement = require('./ArrayElement');
|
---|
| 5 | const MemberElement = require('./MemberElement');
|
---|
| 6 | const ObjectSlice = require('../ObjectSlice');
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * @class
|
---|
| 10 | *
|
---|
| 11 | * @param content
|
---|
| 12 | * @param meta
|
---|
| 13 | * @param attributes
|
---|
| 14 | */
|
---|
| 15 | class ObjectElement extends ArrayElement {
|
---|
| 16 | constructor(content, meta, attributes) {
|
---|
| 17 | super(content || [], meta, attributes);
|
---|
| 18 | this.element = 'object';
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | primitive() {
|
---|
| 22 | return 'object';
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | toValue() {
|
---|
| 26 | return this.content.reduce((results, el) => {
|
---|
| 27 | results[el.key.toValue()] = el.value ? el.value.toValue() : undefined;
|
---|
| 28 | return results;
|
---|
| 29 | }, {});
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * @param key
|
---|
| 34 | * @returns {Element}
|
---|
| 35 | */
|
---|
| 36 | get(name) {
|
---|
| 37 | const member = this.getMember(name);
|
---|
| 38 |
|
---|
| 39 | if (member) {
|
---|
| 40 | return member.value;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return undefined;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * @param key
|
---|
| 48 | * @returns {MemberElement}
|
---|
| 49 | */
|
---|
| 50 | getMember(name) {
|
---|
| 51 | if (name === undefined) { return undefined; }
|
---|
| 52 |
|
---|
| 53 | return this.content.find(element => element.key.toValue() === name);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * @param key
|
---|
| 58 | */
|
---|
| 59 | remove(name) {
|
---|
| 60 | let removed = null;
|
---|
| 61 |
|
---|
| 62 | this.content = this.content.filter((item) => {
|
---|
| 63 | if (item.key.toValue() === name) {
|
---|
| 64 | removed = item;
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return true;
|
---|
| 69 | });
|
---|
| 70 |
|
---|
| 71 | return removed;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @param key
|
---|
| 76 | * @returns {Element}
|
---|
| 77 | */
|
---|
| 78 | getKey(name) {
|
---|
| 79 | const member = this.getMember(name);
|
---|
| 80 |
|
---|
| 81 | if (member) {
|
---|
| 82 | return member.key;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return undefined;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Set allows either a key/value pair to be given or an object
|
---|
| 90 | * If an object is given, each key is set to its respective value
|
---|
| 91 | */
|
---|
| 92 | set(keyOrObject, value) {
|
---|
| 93 | if (isObject(keyOrObject)) {
|
---|
| 94 | Object.keys(keyOrObject).forEach((objectKey) => {
|
---|
| 95 | this.set(objectKey, keyOrObject[objectKey]);
|
---|
| 96 | });
|
---|
| 97 |
|
---|
| 98 | return this;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // Store as key for clarity
|
---|
| 102 | const key = keyOrObject;
|
---|
| 103 | const member = this.getMember(key);
|
---|
| 104 |
|
---|
| 105 | if (member) {
|
---|
| 106 | member.value = value;
|
---|
| 107 | } else {
|
---|
| 108 | this.content.push(new MemberElement(key, value));
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | return this;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | */
|
---|
| 116 | keys() {
|
---|
| 117 | return this.content.map(item => item.key.toValue());
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | */
|
---|
| 122 | values() {
|
---|
| 123 | return this.content.map(item => item.value.toValue());
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * @returns {boolean}
|
---|
| 128 | */
|
---|
| 129 | hasKey(value) {
|
---|
| 130 | return this.content.some(member => member.key.equals(value));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * @returns {array}
|
---|
| 135 | */
|
---|
| 136 | items() {
|
---|
| 137 | return this.content.map(item => [item.key.toValue(), item.value.toValue()]);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * @param callback
|
---|
| 142 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 143 | */
|
---|
| 144 | map(callback, thisArg) {
|
---|
| 145 | return this.content.map(item => callback.bind(thisArg)(item.value, item.key, item));
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * Returns an array containing the truthy results of calling the given transformation with each element of this sequence
|
---|
| 150 | * @param transform - A closure that accepts the value, key and member element of this object as its argument and returns an optional value.
|
---|
| 151 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 152 | * @returns An array of the non-undefined results of calling transform with each element of the array
|
---|
| 153 | */
|
---|
| 154 | compactMap(callback, thisArg) {
|
---|
| 155 | const results = [];
|
---|
| 156 |
|
---|
| 157 | this.forEach((value, key, member) => {
|
---|
| 158 | const result = callback.bind(thisArg)(value, key, member);
|
---|
| 159 |
|
---|
| 160 | if (result) {
|
---|
| 161 | results.push(result);
|
---|
| 162 | }
|
---|
| 163 | });
|
---|
| 164 |
|
---|
| 165 | return results;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * @param callback
|
---|
| 170 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 171 | *
|
---|
| 172 | * @returns {ObjectSlice}
|
---|
| 173 | */
|
---|
| 174 | filter(callback, thisArg) {
|
---|
| 175 | return new ObjectSlice(this.content).filter(callback, thisArg);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * @param callback
|
---|
| 180 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 181 | *
|
---|
| 182 | * @returns {ObjectSlice}
|
---|
| 183 | *
|
---|
| 184 | * @memberof ObjectElement.prototype
|
---|
| 185 | */
|
---|
| 186 | reject(callback, thisArg) {
|
---|
| 187 | return this.filter(negate(callback), thisArg);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * @param callback
|
---|
| 192 | * @param thisArg - Value to use as this (i.e the reference Object) when executing callback
|
---|
| 193 | *
|
---|
| 194 | * @memberof ObjectElement.prototype
|
---|
| 195 | */
|
---|
| 196 | forEach(callback, thisArg) {
|
---|
| 197 | return this.content.forEach(item => callback.bind(thisArg)(item.value, item.key, item));
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | module.exports = ObjectElement;
|
---|