-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
163 lines (91 loc) · 4.61 KB
/
README
File metadata and controls
163 lines (91 loc) · 4.61 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
__
__ ______ ____/ /__ ___________________ ________ _ __
/ / / / __ \/ __ / _ \/ ___/ ___/ ___/ __ \/ ___/ _ \| |/_/
/ /_/ / / / / /_/ / __/ / (__ ) /__/ /_/ / / / __/> <
\__,_/_/ /_/\__,_/\___/_/ /____/\___/\____/_/ \___/_/|_| v0.3.4
http://travis-ci.org/akidee/underscorex
Underscorex are some basic extensions for underscore.js
- extends JavaScript natives to implement missing ECMA5 functionality
- provides additonal basic functionality
- runs in all environments, server and client
- small footprint: 6 kB gzipped
Features
1. Make ECMA5 functionality, which is backwards compatible, available to all JavaScript engines:
Object.keys
Date.prototype.toISOString
Date.prototype.toJSON
Date.now
Array.isArray
JSON.parse/stringify
Function.prototype.bind
String.prototype.trim
Array.prototype.indexOf/lastIndexOf
Array.prototype.every/some/forEach/map/filter
Array.prototype.reduce/reduceRight
Error.prototype.toJSON (exports default properties that are not enumerable)
Functionality that cannot be fully implemented with ECMA3 is not added.
(See http://kangax.github.com/es5-compat-table/ )
2. Functionality beyond ECMA5 is included in underscore.js: http://documentcloud.github.com/underscore
3. My own mixins:
_.log(...)
Alias of console.log. Can be called even if the console object is not defined
_.isRecursable(object)
Returns true if object is recursable - in opposite to instances of Date, Function or simple types
_.extend(object, source, ...)
Overwrites underscore's _.extend to iterate over own properties only
_.extendDeep(object, source, ..., preserve = false)
Extends object with other objects by deep tree recursion. Does not consider circular references. To preserve existing properties of object, pass true as the last argument
_.objectToArray(object)
Transforms an object to a list of lists with 2 elements each (key and value). Example:
_.objectToArray({ a: 1, b: true }) --> [ [ a, 1 ], [ b, true ] ]
Applying Array.prototype functionality to objects is very useful, but underscore's collection utilities return an array of the values of the passed objects and omit the keys. But we don't want to miss the keys and filter, sort, ... by them as well.
For example, you can sort an object by keys by sorting the result of _.objectToArray(object) and then transform it back to an object with _.arrayToObject. The ECMA standard does _not_ force implementors to iterate over objects in the same order as the properties were defined, but all JS engines will do that for non-numeric properties. See http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop/280861#280861
_.arrayToObject(array)
Transforms a list of lists - with key and value each - back to an object. Example:
_.arrayToObject([ [ a, 1 ], [ 5, true ] ]) --> { 'a': 1, '5': true }
_.escapeXml(string, newline = false)
_.simple(string, preserveCase = false)
Get a simplified version of a string by trimming, reducing whitespace and setting it to lower case, which can be prohibited by passing true as the second argument
_.escapeForRegExp(string)
Escape characters to make a string non-semantic when using it in a regular expression
_.locale(object, locale?)
Examples:
var data = { de_DE: 'Zentrum', en_UK: 'centre', en: 'center' }
_.locale(data, 'de') --> 'Zentrum'
_.locale(data) --> 'center'
_.MAX_INT
Maximum integer in JavaScript: 9007199254740992
See http://www.irt.org/script/1031.htm
_.MIN_INT
0 - _.MAX_INT
Installation
npm install underscorex
In node.js
var _ = require('underscorex')
In the browser (standalone)
/client/standalone/underscorex.js is the minified version
<script src="underscore.js"></script> <!-- underscore.js version of your choice -->
<script src="underscorex.js"></script>
<script>
_.log('ready')
</script>
In the browser (AMD)
The best way to use CommonJS modules with their dependencies on the client is wrapping them and loading them asynchronously with RequireJS. Copy /client/requirejs/underscore.js to the correct path on your server. The file includes underscore.js v1.3.0
Example - definition of your own library with underscorex as a dependency:
<script src="/path/to/require.js"></script>
<script>
require.config({
baseUrl: '/js'
})
reqire([ 'underscorex' ], function (_) {
_.log('ready')
})
</script>
Tested in
node.js 0.4-latest
IE 5.5+, FF 3+, Chrome 1+, Opera 10+, Safari 4+
Mobile Safari 4.0
Used code
https://developer.mozilla.org/en/JavaScript/Reference
https://github.com/kriskowal/es5-shim
http://www.JSON.org/json2.js