High quality JavaScript extensions for various tasks such as formatting dates, currency helpers, and more. This library targets node.js and server-side frameworks such as Express.
Ext.js uses the CommonJS module pattern when needed. Additionally, all of the native object extensions are non-enumerable, and you may create your own via Ext.extend(prototype, methods).
Install the Kiwi package manager for nodejs and run:
$ kiwi install ext
To use simply: require('ext')
- Object#keys
- Object#values
- Object#join([str])
- Object#alias(orig, alias)
- Object#tap(fn[, context])
- Object#merge(obj[, reverse])
- Object#each(fn[, context])
- Object#respondsTo(key)
- Array#each(fn[, context])
- Array#excludes(item[, item, ...])
- Array#includes(item[, item, ...])
- Array#clear
- Array#flattened
- Array#first
- Array#last
- Array#sample
- Array#compact([removableValues])
- Array#at(index)
- Array#drop(n)
- Array#take(n)
- Array#grep(pattern)
- Array#remove(obj[, fn[, context]])
- Array#transposed
- Array#isEmpty
- Array#sum
- Array#avg
- Array#min
- Array#max
- Enumerable#map(fn[, context])
- Enumerable#reduce(fn[, memo]) aliased as inject()
- Enumerable#filter(fn[, context]) aliased as select()
- Enumerable#detect(fn[, context]) aliased as find()
- Enumerable#some(fn[, context]) aliased as any()
- Enumerable#every(fn[, context]) aliased as all()
- Enumerable#none(fn[, context])
- Enumerable#reject(fn[, context])
- Number#ordinalize
- Number#currency
- Number#second / Number#seconds
- Number#minute / Number#minutes
- Number#hour / Number#hours
- Number#day / Number#days
- Number#week / Number#weeks
- Number#month / Number#months
- Number#year / Number#years
- Number#toSeconds
- Number#toMinutes
- Number#toHours
- Number#toDays
- Number#toWeeks
- Number#toMonths
- Number#toYears
- Number#byte / Number#bytes
- Number#kilobyte / Number#kilobytes
- Number#megabyte / Number#megabytes
- Number#gigabyte / Number#gigabytes
- Number#ago
- Number#times(fn[, context])
- Number#isFloat
- String#variable
- String#uppercase
- String#lowercase
- String#camelcase
- String#digits
- String#strip
- String#drop(n)
- String#take(n)
- String#before(str)
- String#after(str)
- String#padLeft(width[, char])
- String#padRight(width[, char])
- String#remove(pattern)
- String#startsWith(str)
- String#endsWith(str)
- String#capitalize([all])
- String#wrap(prefix[, suffix])
- String#singular
- String#plural
- String#isPlural
- String#isSingular
- String#includes(str)
- String#count(str)
- Date#year
- Date#month
- Date#date
- Date#day
- Date#hours
- Date#minutes
- Date#seconds
- Date#milliseconds
- Date#monthName
- Date#shortMonthName
- Date#dayName
- Date#shortDayName
- Date#format(str)
- Date#inWordsSince(date)
- Date#inWordsSinceNow
- Date#parse(str)
- parse(str[, date])
- RegExp.escape(str[, chars])
- Function#bind(context[, ...])
- Function#curry(...)
- String#base64Encode / encode(str)
- String#base64Decode / decode(str)
- Error.raise([name[, message[, object]]])
- String#md5 / hash(str)
- sprintf(str[, arg[, ...]])
- eprintf(str[, arg[, ...]])
- printf(str[, arg[, ...]])
Ext.js allows most iterators to use "string lambda expressions", which allows you to define a function by passing a string containing, a valid Lambda expression or shortform as shown in some of the examples below.
[1,2,3].map('a -> a + 1')
// => [2,3,4]
curried = [1,2,3].map('a -> b -> a + b')
// => [(Function), (Function), (Function)]
curried[1](40)
// => 42
[1,2,3,4,5].select(function(n){ return n > 3 })
// => [4,5]
['hello', 'there'].map('.length')
// => [5,5]
[['hello'], ['there']].map('[0]')
// => ['hello', 'there']
[1,2,3].map('.toString()')
// => ['1','2','3']
[1,2,3,4,5].select('> 3')
// => [4,5]
{ foo: 'bar', baz: 'something' }.select(".length > 3")
// => { baz: 'something' }
The module ext/date exports the parse() function which accepts a string to parse, as well as an optional date which represents "now".
Below are some examples:
new Date
// => Fri, 26 Feb 2010 19:16:47 GMT
parse('today')
// => Fri, 26 Feb 2010 19:16:47 GMT
parse('yesterday')
// => Thu, 25 Feb 2010 19:16:47 GMT
parse('in 5 hours')
// => Sat, 27 Feb 2010 00:16:47 GMT
parse('in 2 days')
// => Sun, 28 Feb 2010 19:16:47 GMT
parse('next tuesday')
// => Tue, 02 Mar 2010 19:16:47 GMT
More examples:
'in a year'
'in one year'
'in five days'
'in three hundred minutes'
'in fifty two minutes'
'in seventy five trillion seconds'
'in five and a half minutes'
$ make test
- TJ Holowaychuk (visionmedia)
- Tobias Svensson (tobiassvn)
These guidlines must be met before commit(s) or patches will be accepted.
- Use 2 space indents
- No trailing whitespace
- Blank line before EOF
- Omit semi-colons unless required (very rarely is this needed)
- Never bump the version
File comments should take the following form:
// ext.js - <category> [- subcategory ...] - Copyright TJ Holowaychuk <[email protected]> (MIT Licensed)
Method comments should take the following form, and 's should be lowercase. "mixed" should be used when multiple types are acceptable, not "object".
When accepting an options object for example, you may use "hash" as this indicates that common usage of this parameter is to pass a literal object using brace syntax.
All examples should be indented 2 spaces.
/**
* <description>
*
* <example ...>
*
* @param {<type>} <name>
* @param {<type>} <name>
* @param {<type>} <name>
* @return {<type>}
* @api <public|private|protected>
*/
No trailing whitespace after hash keys:
{ foo: 'bar' }
// good
{ foo : 'bar' }
// bad
Hash whitespace:
{ foo: 'bar', baz: 'raz' }
// good
{foo:'bar', baz: 'raz'}
// bad
{
foo: 'bar',
baz: 'raz'
}
// good
Chained methods should be indented to indicate context:
str
.strip
.replace(...)
.replace(...)
// good
str
.strip
.replace(...)
.replace(...)
// bad
str.
strip.
replace(...).
replace(...)
// bad
Use single quoted strings when possible:
'yay'
// good
"he said 'yay'"
// good
"yay"
// bad
Large ternary conditional should take the following form:
foo = some.largeProperty === undefined
? 'some large value'
: some.largeProperty + 'whatever'
Conditionals should be functional when possible (I hate braces):
if (foo)
bar(),
baz()
else
if (somethingElse)
whateves()
// goood
if (foo) {
bar()
baz()
}
else {
if (somethingElse) {
whateves()
}
}
// bad
Closures:
function(){
}
// good
function () {
}
// bad
Methods:
foo.bar = function() {
}
// good
foo.bar = function (){
}
// bad
(The MIT License)
Copyright (c) 2009 TJ Holowaychuk <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.