canvas-lms/packages/obj-unflatten/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.8 KiB
JavaScript
Raw Normal View History

Convert some files in /object from .coffee to .js refs: CNVS-37358 most of this conversion was automated by 2 tools, the `amd-to-es6` and `bulk-decaffeinate` npm packages. but see the test plan for the manual changes that were required. Test plan, aka things to check for in code review: * look for errors in the conversion of the AMD `define` block and the es6 `import` statements. * the `amd-to-es6` tool choked on anything that did destructuring so those had to be done manually, eg: make sure `defile([‘underscore’], function({reduce}) {…` gets converted to `import {reduce} from ‘underscore’ (and not the erroneous `import ‘underscore’) * I had to manually convert the cs `for foo, bar in baz` loops because decaffeinate would try to convert them to `for … of` iterator loops which we don’t support. * Keep a close eye on those and where I converted things to .map .forEach or Object.values(…).forEach * I manually removed some `return` statements, especially where Coffee script was building up an array (like when a method ended in a `for foo in bar` loop, which it would return an array with the built up values even though it wasn’t being used. If in doubt, I tried not to remove any `return`s I wasn’t absolutely sure we didn’t need but keep an eye out for those Change-Id: I28a8b86e2a7e5329923214f21f6f64903fd030b8 Reviewed-on: https://gerrit.instructure.com/114592 Tested-by: Jenkins Reviewed-by: Felix Milea-Ciobanu <fmileaciobanu@instructure.com> Product-Review: Ryan Shaw <ryan@instructure.com> QA-Review: Ryan Shaw <ryan@instructure.com>
2017-06-08 02:55:01 +08:00
//
// Copyright (C) 2012 - present Instructure, Inc.
//
// This file is part of Canvas.
//
// Canvas is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, version 3 of the License.
//
// Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
import _ from 'underscore'
// turns {'foo[bar]': 1} into {foo: {bar: 1}}
export default function unflatten(obj) {
Convert some files in /object from .coffee to .js refs: CNVS-37358 most of this conversion was automated by 2 tools, the `amd-to-es6` and `bulk-decaffeinate` npm packages. but see the test plan for the manual changes that were required. Test plan, aka things to check for in code review: * look for errors in the conversion of the AMD `define` block and the es6 `import` statements. * the `amd-to-es6` tool choked on anything that did destructuring so those had to be done manually, eg: make sure `defile([‘underscore’], function({reduce}) {…` gets converted to `import {reduce} from ‘underscore’ (and not the erroneous `import ‘underscore’) * I had to manually convert the cs `for foo, bar in baz` loops because decaffeinate would try to convert them to `for … of` iterator loops which we don’t support. * Keep a close eye on those and where I converted things to .map .forEach or Object.values(…).forEach * I manually removed some `return` statements, especially where Coffee script was building up an array (like when a method ended in a `for foo in bar` loop, which it would return an array with the built up values even though it wasn’t being used. If in doubt, I tried not to remove any `return`s I wasn’t absolutely sure we didn’t need but keep an eye out for those Change-Id: I28a8b86e2a7e5329923214f21f6f64903fd030b8 Reviewed-on: https://gerrit.instructure.com/114592 Tested-by: Jenkins Reviewed-by: Felix Milea-Ciobanu <fmileaciobanu@instructure.com> Product-Review: Ryan Shaw <ryan@instructure.com> QA-Review: Ryan Shaw <ryan@instructure.com>
2017-06-08 02:55:01 +08:00
return _(obj).reduce((newObj, val, key) => {
let keys = key.split('][')
let lastKey = keys.length - 1
// If the first keys part contains [ and the last ends with ], then []
// are correctly balanced.
if (/\[/.test(keys[0]) && /\]$/.test(keys[lastKey])) {
// Remove the trailing ] from the last keys part.
keys[lastKey] = keys[lastKey].replace(/\]$/, '')
// Split first keys part into two parts on the [ and add them back onto
// the beginning of the keys array.
keys = keys
.shift()
.split('[')
.concat(keys)
Convert some files in /object from .coffee to .js refs: CNVS-37358 most of this conversion was automated by 2 tools, the `amd-to-es6` and `bulk-decaffeinate` npm packages. but see the test plan for the manual changes that were required. Test plan, aka things to check for in code review: * look for errors in the conversion of the AMD `define` block and the es6 `import` statements. * the `amd-to-es6` tool choked on anything that did destructuring so those had to be done manually, eg: make sure `defile([‘underscore’], function({reduce}) {…` gets converted to `import {reduce} from ‘underscore’ (and not the erroneous `import ‘underscore’) * I had to manually convert the cs `for foo, bar in baz` loops because decaffeinate would try to convert them to `for … of` iterator loops which we don’t support. * Keep a close eye on those and where I converted things to .map .forEach or Object.values(…).forEach * I manually removed some `return` statements, especially where Coffee script was building up an array (like when a method ended in a `for foo in bar` loop, which it would return an array with the built up values even though it wasn’t being used. If in doubt, I tried not to remove any `return`s I wasn’t absolutely sure we didn’t need but keep an eye out for those Change-Id: I28a8b86e2a7e5329923214f21f6f64903fd030b8 Reviewed-on: https://gerrit.instructure.com/114592 Tested-by: Jenkins Reviewed-by: Felix Milea-Ciobanu <fmileaciobanu@instructure.com> Product-Review: Ryan Shaw <ryan@instructure.com> QA-Review: Ryan Shaw <ryan@instructure.com>
2017-06-08 02:55:01 +08:00
lastKey = keys.length - 1
} else {
// Basic 'foo' style key.
lastKey = 0
}
if (lastKey) {
// Complex key, build deep object structure based on a few rules:
// * The 'cur' pointer starts at the object top-level.
// * [] = array push (n is set to array length), [n] = array if n is
// numeric, otherwise object.
// * If at the last keys part, set the value.
// * For each keys part, if the current level is undefined create an
// object or array based on the type of the next keys part.
// * Move the 'cur' pointer to the next level.
// * Rinse & repeat.
let i = 0
let cur = newObj
while (i <= lastKey) {
key = keys[i] === '' ? cur.length : keys[i]
cur = cur[key] =
i < lastKey ? cur[key] || (keys[i + 1] && isNaN(keys[i + 1]) ? {} : []) : val
Convert some files in /object from .coffee to .js refs: CNVS-37358 most of this conversion was automated by 2 tools, the `amd-to-es6` and `bulk-decaffeinate` npm packages. but see the test plan for the manual changes that were required. Test plan, aka things to check for in code review: * look for errors in the conversion of the AMD `define` block and the es6 `import` statements. * the `amd-to-es6` tool choked on anything that did destructuring so those had to be done manually, eg: make sure `defile([‘underscore’], function({reduce}) {…` gets converted to `import {reduce} from ‘underscore’ (and not the erroneous `import ‘underscore’) * I had to manually convert the cs `for foo, bar in baz` loops because decaffeinate would try to convert them to `for … of` iterator loops which we don’t support. * Keep a close eye on those and where I converted things to .map .forEach or Object.values(…).forEach * I manually removed some `return` statements, especially where Coffee script was building up an array (like when a method ended in a `for foo in bar` loop, which it would return an array with the built up values even though it wasn’t being used. If in doubt, I tried not to remove any `return`s I wasn’t absolutely sure we didn’t need but keep an eye out for those Change-Id: I28a8b86e2a7e5329923214f21f6f64903fd030b8 Reviewed-on: https://gerrit.instructure.com/114592 Tested-by: Jenkins Reviewed-by: Felix Milea-Ciobanu <fmileaciobanu@instructure.com> Product-Review: Ryan Shaw <ryan@instructure.com> QA-Review: Ryan Shaw <ryan@instructure.com>
2017-06-08 02:55:01 +08:00
i++
}
} else {
// Simple key, even simpler rules, since only scalars and shallow
// arrays are allowed.
if (_.isArray(newObj[key])) {
// val is already an array, so push on the next value.
newObj[key].push(val)
} else if (newObj[key] != null) {
// val isn't an array, but since a second value has been specified,
// convert val into an array.
newObj[key] = [newObj[key], val]
} else {
// val is a scalar.
newObj[key] = val
}
}
return newObj
}, Object.create(null))
Convert some files in /object from .coffee to .js refs: CNVS-37358 most of this conversion was automated by 2 tools, the `amd-to-es6` and `bulk-decaffeinate` npm packages. but see the test plan for the manual changes that were required. Test plan, aka things to check for in code review: * look for errors in the conversion of the AMD `define` block and the es6 `import` statements. * the `amd-to-es6` tool choked on anything that did destructuring so those had to be done manually, eg: make sure `defile([‘underscore’], function({reduce}) {…` gets converted to `import {reduce} from ‘underscore’ (and not the erroneous `import ‘underscore’) * I had to manually convert the cs `for foo, bar in baz` loops because decaffeinate would try to convert them to `for … of` iterator loops which we don’t support. * Keep a close eye on those and where I converted things to .map .forEach or Object.values(…).forEach * I manually removed some `return` statements, especially where Coffee script was building up an array (like when a method ended in a `for foo in bar` loop, which it would return an array with the built up values even though it wasn’t being used. If in doubt, I tried not to remove any `return`s I wasn’t absolutely sure we didn’t need but keep an eye out for those Change-Id: I28a8b86e2a7e5329923214f21f6f64903fd030b8 Reviewed-on: https://gerrit.instructure.com/114592 Tested-by: Jenkins Reviewed-by: Felix Milea-Ciobanu <fmileaciobanu@instructure.com> Product-Review: Ryan Shaw <ryan@instructure.com> QA-Review: Ryan Shaw <ryan@instructure.com>
2017-06-08 02:55:01 +08:00
}