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