forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunflatten.coffee
More file actions
68 lines (58 loc) · 2.22 KB
/
Copy pathunflatten.coffee
File metadata and controls
68 lines (58 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# turns {'foo[bar]': 1} into {foo: {bar: 1}}
define ['underscore'], (_) ->
unflatten = (obj) ->
_(obj).reduce (newObj, val, key) ->
keys = key.split('][')
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)
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.
i = 0
cur = newObj
while i <= lastKey
key = if keys[i] is ""
cur.length
else
keys[i]
cur = cur[key] = if i < lastKey
cur[key] or if keys[i + 1] and isNaN(keys[i + 1])
{}
else
[]
else
val
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]?
# 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
, {}