underscorex are some basic extensions for underscore.js
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
client
example
test
.gitignore
.npmignore
.travis.yml
CHANGELOG
LICENSE
README
TODO
index.js
package.json

README

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