Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

underscorex

0.3.4 • Public • Published
               __

__ ______ / / ___________________ ________ _ __ / / / / __ / __ / _ / / / / __ / / _ | |// / // / / / / // / / / ( ) // // / / / __/> < _,// //_,/___// //_/_// ___//|_| 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/ )

  1. Functionality beyond ECMA5 is included in underscore.js: http://documentcloud.github.com/underscore

  2. 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

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:

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

Keywords

none

Install

npm i underscorex

DownloadsWeekly Downloads

9

Version

0.3.4

License

none

Last publish

Collaborators

  • avatar