1 | module.exports =
|
2 |
|
3 | isEmpty: (val) ->
|
4 | unless val?
|
5 | return false
|
6 | if Object.isArray(val) or Object.isString(val)
|
7 | return val.length is 0
|
8 | return val.keys.length is 0
|
9 |
|
10 | isArray: (val) -> Array.isArray val
|
11 | isElement: (val) -> val.nodeType is 1
|
12 | isBoolean: (val) -> typeof val is 'boolean'
|
13 | isObject: (val) -> typeof val is 'object'
|
14 | isNumber: (val) -> typeof val is 'number'
|
15 | isString: (val) -> typeof val is 'string'
|
16 | isFunction: (val) -> typeof val is 'function'
|
17 | isXML: (val) -> typeof val is 'xml'
|
18 |
|
19 | getKey: -> @keys()[0]
|
20 | getValue: -> @[@getKey()]
|
21 | getKeys: (value) -> x for x in @keys() when @[x] is value
|
22 | getValues: (key) -> @[x] for x in @keys() when x is key
|
23 | keys: -> Object.keys @
|
24 | values: -> @[x] for x in @keys()
|
25 | remove: (key) -> delete @[key]; @
|
26 |
|
27 | stringify: -> JSON.stringify @
|
28 | prettify: -> JSON.stringify @, null, 4
|
29 |
|
30 | merge: (source, overwrite, deep) ->
|
31 | overwrite ?= false
|
32 | deep ?= false
|
33 | if deep
|
34 | for name in Object.getOwnPropertyNames source
|
35 | if @hasOwnProperty name and !overwrite then continue
|
36 | Object.defineProperty @, name, Object.getOwnPropertyDescriptor(source, name)
|
37 | else
|
38 | for x in source.keys()
|
39 | if @hasOwnProperty x and !overwrite then continue
|
40 | @[x] = source[x]
|
41 | return @
|
42 |
|
43 | concat: (source) -> @merge source, false, false
|
44 |
|
45 | extend: (source) -> @merge source
|
46 |
|
47 | clone: ->
|
48 | out = {}
|
49 | for name in Object.getOwnPropertyNames @
|
50 | Object.defineProperty out, name, Object.getOwnPropertyDescriptor(@, name)
|
51 | return out
|
52 |
|
53 | clear: ->
|
54 | for x in @keys()
|
55 | @remove(x)
|
56 | return @
|
57 |
|
58 | unique: (merge) ->
|
59 | out = {}
|
60 | merge ?= false
|
61 | for x in @keys()
|
62 | if !out.hasOwnProperty x
|
63 | if merge
|
64 | vals = @getValues(x)
|
65 | if vals.length > 1 then out[x] = vals else out[x] = vals[0]
|
66 | else
|
67 | out[x] = @[x]
|
68 | return out
|
69 |
|
70 | for key of module.exports
|
71 | Object.defineProperty Object.prototype, key, value: module.exports[key]
|