UNPKG

5.79 kBMarkdownView Raw
1**Protege is a versatile NodeJS extension library that aims to make code as simple as possible**
2
3## Installation
4
5To install Protege, use [npm](http://github.com/isaacs/npm):
6
7 $ npm install protege
8
9## Usage
10
11```require('protege')``` - it's that easy
12
13### Object
14####These apply to all types, some are Object specific though
15```
16.clone() - Duplicates the object into a new reference
17.clear() - Removes all keys and values from the objects
18.concat(source) - Adds all properties/values from source to object.
19 (optional) overwrite = true will overwrite existing values with source values
20.merge(source, overwrite, deep) - Merge source properties/values into object.
21 (optional) overwrite = true will overwrite existing values with source values
22 (optional) deep = true will initiate a deep merge, merging descriptors and non-enumerable properties
23
24.extend(source) - Alias for merge
25.unique(merge) - Returns a unique copy of the object. (optional) merge will merge values for duplicate keys into arrays instead of removing them
26
27Object.isEmpty(object) - Returns true if object is empty
28Object.isObject(object) - Returns true if object is an object
29Object.isArray(object) - Returns true if object is an array
30Object.isElement(object) - Returns true if object is a DOM element
31Object.isBoolean(object) - Returns true if object is a boolean
32Object.isNumber(object) - Returns true if object is a number
33Object.isString(object) - Returns true if object is a string
34Object.isFunction(object) - Returns true if object is a function
35Object.isXML(object) - Returns true if object is an XML object
36
37.keys() - Returns all keys in the object
38.values() - Returns all values in the object
39
40.getKey() - Returns first key in object
41.getValue() - Returns first value in object
42.getKeys(value) - Returns all keys for the corresponding value
43.getValues(key) - Returns all values for the corresponding key
44
45.remove(key) - Removes key from object
46
47.stringify() - Returns JSON string for object
48.prettify() - Returns a human readable JSON string for object
49```
50
51### Array
52####These also apply to String (since String is just an array after all) but some are only really useful for true arrays
53```
54.clone() - Duplicates the array into a new reference.
55
56.merge(arr) - Adds array arr to array
57.unique() - Returns a copy of the array with duplicates removed
58.flatten() - Returns a one-dimensional copy of the array
59.compact() - Returns a copy of the array with all undefined and null items removed
60.intersect(arr) - Returns an array of all items in both arr and the array
61
62.size() - Returns size of array
63.first() - Returns first value or array or undefined
64.last() - Returns last value or array or undefined
65
66.replace(value, newval) - Replaces first instance of value with newval
67.replaceIndex(index, value) - Replaces item at index with value
68.replaceAll(value, newval) - Replaces all instances of value with newval
69.remove(value) - Removes first instance of value from array
70.removeAll(value) - Removes all instances of value from array.
71.removeAllIgnoreCase(value) - Removes all instances of value from array ignoring case
72.removeIndex(idx) - Removes index of idx from array
73```
74
75### String
76```
77.upcase() - Alias for toUpperCase(), inspired by Ruby syntax
78.downcase() - Alias for toLowerCase(), inspired by Ruby syntax
79
80.strip() - Alias for trim(), inspired by Ruby syntax
81.rstrip() - Alias for trimRight(), inspired by Ruby syntax
82.lstrip() - Alias for trimLeft(), inspired by Ruby syntax
83
84.replaceAll(value, newval) - Replaces all instances of value with newval
85
86.startsWith(value) - Returns true if string starts with value, inspired by Java syntax
87.endsWith(value) - Returns true if string ends with value, inspired by Java syntax
88.startsWithIgnoreCase(value) - Returns true if string starts with value ignoring case, inspired by Java syntax
89.endsWithIgnoreCase(value) - Returns true if string ends with value ignoring case, inspired by Java syntax
90
91.contains(value) - Returns true if the string contains value, inspired by Java syntax
92.containsIgnoreCase(value) - Returns true if the string contains value ignoring case, inspired by Java syntax
93.equalsIgnoreCase(value) - Returns true if the string equals value ignoring case, inspired by Java syntax
94```
95
96### Function
97```
98.memoize() - Caches result for parameters so any future calls will skip execution and return previous result
99 Example: var cacheFunc = myFunc.memoize(); cacheFunc(15); cacheFunc(13); cacheFunc(15);
100 Only the first two calls will actually be executed.
101```
102## Examples
103
104You can view further examples in the [example folder.](https://github.com/wearefractal/protege/tree/master/examples)
105
106## LICENSE
107
108(MIT License)
109
110Copyright (c) 2011 Fractal <contact@wearefractal.com>
111
112Permission is hereby granted, free of charge, to any person obtaining
113a copy of this software and associated documentation files (the
114"Software"), to deal in the Software without restriction, including
115without limitation the rights to use, copy, modify, merge, publish,
116distribute, sublicense, and/or sell copies of the Software, and to
117permit persons to whom the Software is furnished to do so, subject to
118the following conditions:
119
120The above copyright notice and this permission notice shall be
121included in all copies or substantial portions of the Software.
122
123THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
124EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
125MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
126NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
127LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
128OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
129WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
130
131
\No newline at end of file