|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +const { unique } = require('../../support/array'); |
| 3 | + |
| 4 | +const destructureObject = { |
| 5 | + description: 'Destructuring an Object', |
| 6 | + codeSample: 'const { a, b } = obj', |
| 7 | + keywords: [ |
| 8 | + 'assignment', |
| 9 | + 'object', |
| 10 | + 'destructuring', |
| 11 | + 'decomposition', |
| 12 | + 'composition' |
| 13 | + ].sort(), |
| 14 | + f: (d) => { |
| 15 | + const { num, obj } = d; |
| 16 | + }, |
| 17 | + testDataType: 'object' |
| 18 | +}; |
| 19 | + |
| 20 | +const destructureObjectDefault = { |
| 21 | + description: 'Destructuring an Object with default values', |
| 22 | + codeSample: 'const { a = i } = obj', |
| 23 | + keywords: [ |
| 24 | + 'assignment', |
| 25 | + 'object', |
| 26 | + 'destructuring', |
| 27 | + 'decomposition', |
| 28 | + 'composition', |
| 29 | + 'default', |
| 30 | + 'values' |
| 31 | + ].sort(), |
| 32 | + f: (d) => { |
| 33 | + const { num = 5, foo = 'bar' } = d; |
| 34 | + }, |
| 35 | + testDataType: 'object' |
| 36 | +}; |
| 37 | + |
| 38 | +const destructureArray = { |
| 39 | + description: 'Destructuring an Array', |
| 40 | + codeSample: 'const [a,b] = arr', |
| 41 | + keywords: [ |
| 42 | + 'assignment', |
| 43 | + 'array', |
| 44 | + 'destructuring', |
| 45 | + 'decomposition', |
| 46 | + 'composition' |
| 47 | + ].sort(), |
| 48 | + f: (d) => { |
| 49 | + const [a, b] = d; |
| 50 | + }, |
| 51 | + testDataType: 'array' |
| 52 | +}; |
| 53 | + |
| 54 | +const destructureArrayDefault = { |
| 55 | + description: 'Destructuring an Array with default values', |
| 56 | + codeSample: 'const [a = i, b] = arr', |
| 57 | + keywords: [ |
| 58 | + 'assignment', |
| 59 | + 'array', |
| 60 | + 'destructuring', |
| 61 | + 'decomposition', |
| 62 | + 'composition', |
| 63 | + 'default', |
| 64 | + 'values' |
| 65 | + ].sort(), |
| 66 | + f: (d) => { |
| 67 | + const [a = 5, b] = d; |
| 68 | + }, |
| 69 | + testDataType: 'array' |
| 70 | +}; |
| 71 | + |
| 72 | +const destructureArrayTail = { |
| 73 | + description: 'Destructuring an Array with tail', |
| 74 | + codeSample: 'const [a,b, ...tail] = arr', |
| 75 | + keywords: [ |
| 76 | + 'assignment', |
| 77 | + 'array', |
| 78 | + 'destructuring', |
| 79 | + 'decomposition', |
| 80 | + 'composition', |
| 81 | + 'rest', |
| 82 | + 'tail' |
| 83 | + ].sort(), |
| 84 | + f: (d) => { |
| 85 | + const [a, b, ...tail] = d; |
| 86 | + }, |
| 87 | + testDataType: 'array' |
| 88 | +}; |
| 89 | + |
| 90 | +const assignArray = { |
| 91 | + description: 'Assignment from array items', |
| 92 | + codeSample: 'const a = arr[i]', |
| 93 | + keywords: [ |
| 94 | + 'array', |
| 95 | + 'assignment', |
| 96 | + 'decomposition', |
| 97 | + 'composition' |
| 98 | + ].sort(), |
| 99 | + f: (d) => { |
| 100 | + const a = d[0]; |
| 101 | + const b = d[1]; |
| 102 | + }, |
| 103 | + testDataType: 'array' |
| 104 | +}; |
| 105 | + |
| 106 | +const assignArrayDefault = { |
| 107 | + description: 'Assignment from array items with default', |
| 108 | + codeSample: 'const a = arr[i] || j', |
| 109 | + keywords: [ |
| 110 | + 'array', |
| 111 | + 'assignment', |
| 112 | + 'decomposition', |
| 113 | + 'composition', |
| 114 | + 'default', |
| 115 | + 'values' |
| 116 | + ].sort(), |
| 117 | + f: (d) => { |
| 118 | + const a = d[0] || 5; |
| 119 | + const b = d[1]; |
| 120 | + }, |
| 121 | + testDataType: 'array' |
| 122 | +}; |
| 123 | + |
| 124 | +const assignObject = { |
| 125 | + description: 'Assignment from object properties', |
| 126 | + codeSample: 'const a = obj.b', |
| 127 | + keywords: [ |
| 128 | + 'object', |
| 129 | + 'assignment', |
| 130 | + 'decomposition', |
| 131 | + 'composition' |
| 132 | + ].sort(), |
| 133 | + f: (d) => { |
| 134 | + const str = d.obj.str; |
| 135 | + }, |
| 136 | + testDataType: 'object' |
| 137 | +}; |
| 138 | + |
| 139 | +const assignObjectDefault = { |
| 140 | + description: 'Assignment from object properties with default', |
| 141 | + codeSample: 'const a = obj.b || i', |
| 142 | + keywords: [ |
| 143 | + 'object', |
| 144 | + 'assignment', |
| 145 | + 'decomposition', |
| 146 | + 'composition', |
| 147 | + 'default', |
| 148 | + 'values' |
| 149 | + ].sort(), |
| 150 | + f: (d) => { |
| 151 | + const str = d.obj.foo || 'bar'; |
| 152 | + }, |
| 153 | + testDataType: 'object' |
| 154 | +}; |
| 155 | + |
| 156 | +const destructureSwapArray = { |
| 157 | + description: 'Swapping variables via Array destructuring', |
| 158 | + codeSample: 'const [a, b] = [b, a]', |
| 159 | + keywords: [ |
| 160 | + 'array', |
| 161 | + 'destructuring', |
| 162 | + 'swap', |
| 163 | + 'variables', |
| 164 | + 'composition', |
| 165 | + 'decomposition' |
| 166 | + ], |
| 167 | + f: (d) => { |
| 168 | + let a = d[0]; |
| 169 | + let b = d[1]; |
| 170 | + [a, b] = [b, a]; |
| 171 | + }, |
| 172 | + testDataType: 'array' |
| 173 | +}; |
| 174 | + |
| 175 | +const assignSwapArray = { |
| 176 | + description: 'Swapping variables via assignment', |
| 177 | + codeSample: 'const c = b; b = a; a = c;', |
| 178 | + keywords: [ |
| 179 | + 'swap', |
| 180 | + 'variables', |
| 181 | + 'composition', |
| 182 | + 'decomposition' |
| 183 | + ], |
| 184 | + f: (d) => { |
| 185 | + let a = d[0]; |
| 186 | + let b = d[1]; |
| 187 | + const c = b; |
| 188 | + b = a; |
| 189 | + a = c; |
| 190 | + }, |
| 191 | + testDataType: 'array' |
| 192 | +}; |
| 193 | + |
| 194 | +const functions = [ |
| 195 | + destructureArray, |
| 196 | + destructureArrayDefault, |
| 197 | + destructureArrayTail, |
| 198 | + destructureObject, |
| 199 | + destructureObjectDefault, |
| 200 | + destructureSwapArray, |
| 201 | + assignArray, |
| 202 | + assignArrayDefault, |
| 203 | + assignObject, |
| 204 | + assignObjectDefault, |
| 205 | + assignSwapArray |
| 206 | +]; |
| 207 | + |
| 208 | +module.exports = { |
| 209 | + name: '(de-)composition', |
| 210 | + description: { |
| 211 | + short: '(De-)composing objects, variables and arrays.', |
| 212 | + long: '(De-)composing objects, variables and arrays from each other.' |
| 213 | + }, |
| 214 | + keywords: unique( |
| 215 | + functions.map((fn) => fn.keywords) |
| 216 | + .reduce((keywords, fnKeywords) => [...keywords, ...fnKeywords]) |
| 217 | + ).sort(), |
| 218 | + functions |
| 219 | +}; |
| 220 | +/* eslint-enable no-unused-vars */ |
0 commit comments