mirror of
https://github.com/shimataro/ssh-key-action.git
synced 2025-06-19 22:52:10 +10:00
* first action! (#1)
This commit is contained in:
parent
8deacc95b1
commit
ace1e6a69a
3750 changed files with 1155519 additions and 0 deletions
9
node_modules/cint/.jshintrc
generated
vendored
Normal file
9
node_modules/cint/.jshintrc
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"validthis": true,
|
||||
"smarttabs": true,
|
||||
"asi": true,
|
||||
"globals": [
|
||||
"module",
|
||||
"require"
|
||||
]
|
||||
}
|
2
node_modules/cint/.npmignore
generated
vendored
Normal file
2
node_modules/cint/.npmignore
generated
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
npm-debug.log
|
5
node_modules/cint/.travis.yml
generated
vendored
Normal file
5
node_modules/cint/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- '0.10'
|
||||
notifications:
|
||||
email: false
|
13
node_modules/cint/DEPLOY.md
generated
vendored
Normal file
13
node_modules/cint/DEPLOY.md
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Deployment Steps
|
||||
Add tests
|
||||
npm test
|
||||
Update README
|
||||
Add and commit all changes
|
||||
Manually bumb version number in package.json
|
||||
Update HISTORY
|
||||
gulp
|
||||
git add -A
|
||||
git commit -m "vX.X.X"
|
||||
git tag vX.X.X
|
||||
git push && git push --tags
|
||||
npm publish
|
52
node_modules/cint/HISTORY.md
generated
vendored
Normal file
52
node_modules/cint/HISTORY.md
generated
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
# History
|
||||
|
||||
## 8.2.1
|
||||
- Fix toAsync bug and patch tests
|
||||
|
||||
## 8.2.0
|
||||
- Add toAsync
|
||||
- Convert tests from QUnit to Mocha
|
||||
|
||||
## 8.1.0
|
||||
- Add getKey
|
||||
|
||||
## 8.0.0
|
||||
- Rename joinObj to joinObject
|
||||
- Simplify compareProperty. Use cint.compareProperty.bind(cint, property) to achieve old behavior.
|
||||
|
||||
## 7.2.0
|
||||
- Add unit tests for startsWith, before, after, between, repeatString, toTitleCase, ordinal, mapNumber, rotate, toObject, joinObj, toArray, filterObject, look, compare, compareProperty, isValue, typeof
|
||||
- Added tallyProps.
|
||||
- Rearranged sections.
|
||||
|
||||
## 7.1.2
|
||||
- Removed semicolons. Enabled header in minified file.
|
||||
|
||||
## 7.1.1
|
||||
- Updated README
|
||||
|
||||
## 7.1.0
|
||||
- Added functions: addTwo, add, intoString, setValue, mapOverKey, tap, look
|
||||
|
||||
## 7.0.0
|
||||
- Corrected spelling of 'arritize' to 'aritize'
|
||||
|
||||
## 6.0.0
|
||||
- Removed compose, contains, filter, find, group, isEmpty, merged, pluck, range. Use underscore functions of the same name.
|
||||
- Removed I. Use _.identity.
|
||||
- Removed findByProperty. Use _.findWhere.
|
||||
- Removed filterBy. Use _.where.
|
||||
- Removed hasKey. Use _.has.
|
||||
- Removed sequence. Use _.compose.apply(args.reverse())).
|
||||
- Renamed curryAt to partialAt to more closely match lodash semantics.
|
||||
- Removed curry and rcurry. Use _.partial and _.partialRight.
|
||||
- Removed currify. Use _.curry.
|
||||
- Removed toInstance, install, and installPrototypes. Use [nativity-cint](https://github.com/metaraine/nativity-cint).
|
||||
- Removed reversed. Use arr.concat().reverse().
|
||||
- Renamed hasValue to isValue
|
||||
|
||||
- Removed distributions. Use git tags to checkout a specific version.
|
||||
|
||||
## 6.1.0
|
||||
- Add node-qunit
|
||||
- Remove 'var' from cint declaration to work with node-qunit
|
280
node_modules/cint/README.md
generated
vendored
Normal file
280
node_modules/cint/README.md
generated
vendored
Normal file
|
@ -0,0 +1,280 @@
|
|||
A Javascript utility belt with an emphasis on Functional Programming.
|
||||
|
||||
[](http://badge.fury.io/js/cint) [<img src="https://travis-ci.org/metaraine/cint.svg">](https://travis-ci.org/metaraine/cint)
|
||||
|
||||
# Usage
|
||||
|
||||
Read the API (below) or check out some examples used in the [unit tests](https://github.com/metaraine/cint/blob/master/test/index.html).
|
||||
|
||||
# Installation
|
||||
|
||||
## Client-Side
|
||||
|
||||
Bower:
|
||||
|
||||
bower install cint --save
|
||||
|
||||
Include it:
|
||||
|
||||
<script src="bower_components/cint/cint.min.js"></script>
|
||||
|
||||
## Server-Side
|
||||
|
||||
Install it:
|
||||
|
||||
npm install cint --save
|
||||
|
||||
Require it:
|
||||
|
||||
var cint = require("cint");
|
||||
|
||||
# Unit Tests
|
||||
|
||||
npm test
|
||||
|
||||
# API
|
||||
|
||||
## Function
|
||||
|
||||
/** Returns a function that returns the inverse of the given boolean function. */
|
||||
cint.not(f)
|
||||
|
||||
/** Returns a new function that inserts the given curried arguments to the inner function at the specified index of its runtime arguments.
|
||||
i.e. _.partial(f, args...) is equivalent to cint.partialAt(f, 0, args) and _.partialRight(f, args...) is equivalent to cint.partialAt(f, n, args) where n is the arity of the function.
|
||||
*/
|
||||
cint.partialAt(f, index, curriedArgs)
|
||||
|
||||
/** Returns a new function that calls the given function with a limit on the number of arguments. */
|
||||
cint.aritize(f, n)
|
||||
|
||||
/** Recursively invokes the given function with no parameters until it returns a non-functional value. */
|
||||
cint.callTillValue(value)
|
||||
|
||||
/** Calls the given function as normal, then passes its inputs and output to the spier (defaults to console.log) */
|
||||
cint.spy(f, spier)
|
||||
|
||||
/** Returns a copy of the given function that calls the original function in the context of the first argument. Passes arguments 1..n as normal. */
|
||||
cint.inContext(f)
|
||||
|
||||
/** Converts the given synchronous function into an asynchronous function that applies its arguments to the original function and invokes callback(error, result) */
|
||||
cint.toAsync(f)
|
||||
|
||||
## String
|
||||
|
||||
/** Performs variable substitution on the string, replacing items in {curly braces}.
|
||||
Based on supplant by Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
*/
|
||||
cint.supplant(str, o)
|
||||
|
||||
/** Returns true if the string starts with the given substring. */
|
||||
cint.startsWith(str, sub)
|
||||
|
||||
/** Returns the substring before the first instance of the given delimiter. */
|
||||
cint.before(str, delim)
|
||||
|
||||
/** Returns the substring after the first instance of the given delimiter. Returns the whole string if the delimiter is not found. */
|
||||
cint.after(str, delim)
|
||||
|
||||
/** Returns the substring between the given delimiters. */
|
||||
cint.betweenstr, left, right)
|
||||
|
||||
/** Wraps a string with a left and right */
|
||||
cint.bookend(middle, left, right)
|
||||
|
||||
/** Returns a single string that repeats the string n times. */
|
||||
cint.repeatString(str, n, delim)
|
||||
|
||||
/** Capitalizes the first letter of each word in the given string. */
|
||||
cint.toTitleCase(str)
|
||||
|
||||
## Number
|
||||
|
||||
/** Returns the ordinal value (like '1st' or '2nd') of the given integer. */
|
||||
cint.ordinal(n)
|
||||
|
||||
/** Invokes the given function n times, passing the index for each invocation, and returns an array of the results. */
|
||||
cint.mapNumber(n, f)
|
||||
|
||||
## Array
|
||||
|
||||
/** Returns a list of values plucked from the property from the given array. If the values are functions, they wll be bound to the array item. */
|
||||
cint.pluck(arr, property)
|
||||
|
||||
/** Group the array of objects by one of the object's properties or mappable function. Returns a dictionary containing the original array's items indexed by the property value. */
|
||||
cint.group(arr, propOrFunc)
|
||||
|
||||
/** Group the array of objects by one of the object's properties or mappable function. Returns an array of { key: ___, items: ___ } objects which represent all the items in the original array grouped by the value of the specified grouping key. */
|
||||
cint.orderedGroup(arr, propOrFunc)
|
||||
|
||||
/** Returns a dictionary whose keys are the values of the array and values are the number of instances of that value within the array. */
|
||||
cint.tally(arr)
|
||||
|
||||
/** Tally the property values of an array of object, grouping the counts for each property under its value.
|
||||
e.g.
|
||||
[
|
||||
{
|
||||
ideal: 4,
|
||||
past: 3,
|
||||
present: 7
|
||||
},
|
||||
{
|
||||
ideal: 5,
|
||||
past: 7,
|
||||
present: 7
|
||||
}
|
||||
]
|
||||
|
||||
=>
|
||||
|
||||
{
|
||||
"4": {
|
||||
ideal: 1
|
||||
},
|
||||
"3": {
|
||||
past: 1
|
||||
}
|
||||
"5": {
|
||||
ideal: 1
|
||||
}
|
||||
"7": {
|
||||
present: 2,
|
||||
past: 1
|
||||
}
|
||||
}
|
||||
*/
|
||||
cint.tallyProps(arr)
|
||||
|
||||
/** Returns the unique values in the array. */
|
||||
cint.unique(arr)
|
||||
|
||||
/** Returns the reverse of the given array. Unlike the native reverse, does not modify the original array. */
|
||||
cint.reversed(arr)
|
||||
|
||||
/** Indexes into an array, supports negative indices. */
|
||||
cint.index(arr, i)
|
||||
|
||||
/** Returns a new array containing the elements of the given array shifted n spaces to the left, wrapping around the end. */
|
||||
cint.rotate(arr, n)
|
||||
|
||||
/** Creates an object with a property for each element of the given array, determined by a function that returns the property as a { key: value }. */
|
||||
cint.toObject(arr, f)
|
||||
|
||||
/** Returns the first item in the given array that returns true for the given function. If no item is found, returns null. */
|
||||
cint.find(arr, f)
|
||||
|
||||
/** Returns the first item in the given array whose specified property matches the given value. */
|
||||
cint.findByProperty(arr, prop, value)
|
||||
|
||||
/** Functional, nondestructive version of Array.prototype.splice. */
|
||||
cint.spliced(arr, index, howMany/*, elements*/
|
||||
|
||||
/** Returns an array of sequential integers from start to end (inclusive). If only one parameter is specified, start is 1. */
|
||||
cint.range(start, end)
|
||||
|
||||
/** Returns a new array that only includes items with a specific value of a given property. */
|
||||
cint.filterBy(arr, prop, value)
|
||||
|
||||
/** Breaks up the array into n evenly-sized chunks.
|
||||
Solution from http://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays
|
||||
*/
|
||||
cint.chunk(a, n)
|
||||
|
||||
## Object
|
||||
|
||||
/** Returns an array of the object's values. */
|
||||
cint.values(o)
|
||||
|
||||
/** Returns a new object with the given key and value. */
|
||||
cint.keyValue(key, value)
|
||||
|
||||
/** Gets the value of a key of the given object. */
|
||||
cint.getValue(o, key)
|
||||
|
||||
/** Sets the value of the given key and returns the object. */
|
||||
cint.setValue(o, key, value)
|
||||
|
||||
/** Creates a mapping function that applies the given function to the value of the specified key. */
|
||||
cint.mapOverKey(f, originalKey, newKey)
|
||||
|
||||
/** Join the object into a single string with the given separators separating properties from each other as well as values. */
|
||||
cint.joinObject(obj, propSeparator, valueSeparator)
|
||||
|
||||
/** Returns true if the object has no non-undefined properties.
|
||||
@author Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
*/
|
||||
cint.isEmpty(o)
|
||||
|
||||
/** Returns the number of properties on the given object. */
|
||||
cint.numProperties(o)
|
||||
|
||||
/** Returns a new object with the given objects merged onto it. Non-undefined properties on later arguments override identical properties on earlier arguments. */
|
||||
cint.merge(/*obj1, obj2, obj3, ...*/)
|
||||
|
||||
/** Returns a new object where f(key, value) returns a new key-value pair for each property. */
|
||||
cint.mapObject(obj, f)
|
||||
|
||||
/** Returns an array whose items are the result of calling f(key, value) on each property of the given object. If f is undefined, returns a list of { key: ___, value: ___ } objects. */
|
||||
cint.toArray(obj, f)
|
||||
|
||||
/** Returns a new object that only includes the properties of the given obj for which f(key, value) is true. */
|
||||
cint.filterObject(obj, f)
|
||||
|
||||
/** Changes the specified keys in an object.
|
||||
@example cint.changeKeys(
|
||||
{ fname: 'Raine', lname: 'Lourie', specialty: 'Javascript' },
|
||||
{ fname: 'first', lname: 'last' }
|
||||
)
|
||||
*/
|
||||
cint.changeKeys(obj, changedKeys)
|
||||
|
||||
/** Calls a function on an object and returns the object (for chaining purposes). */
|
||||
cint.tap(f, o)
|
||||
|
||||
/* console.log's the given object and returns the object (for chaining purposes). */
|
||||
cint.look(o)
|
||||
|
||||
## Utility
|
||||
|
||||
/** Adds two numbers. */
|
||||
cint.addTwo(x, y)
|
||||
|
||||
/** Adds all given arguments. */
|
||||
cint.add(/*x,y,z,...*/)
|
||||
|
||||
/** Compares two items lexigraphically. Returns 1 if a>b, 0 if a==b, or -1 if a<b. */
|
||||
cint.compare(a,b)
|
||||
|
||||
/** Returns a function that compares the given property of two items. */
|
||||
cint.compareProperty(property)
|
||||
|
||||
/** Returns a compare function that can be passed to Array.sort to sort in the order of the given array of properties. A property can also be appended with ' ASC' or ' DESC' to control the sort order.
|
||||
*/
|
||||
cint.dynamicCompare(props)
|
||||
|
||||
/** Returns true if all the items in a are equal to all the items in b, recursively. */
|
||||
cint.equals(a,b)
|
||||
|
||||
/** in operator as a function. */
|
||||
cint.hasKey(creamFilling, donut)
|
||||
|
||||
/** Returns true if the given value is not undefined, null, or an empty string. */
|
||||
cint.hasValue(x)
|
||||
|
||||
/** Returns a string representation of the given scalar, array, or dictionary. */
|
||||
cint.hash(o)
|
||||
|
||||
/** Generates a pseudo-random string that can be assumed to be unique.
|
||||
@remarks untestable
|
||||
*/
|
||||
cint.guid()
|
||||
|
||||
/** Returns a string representing the type of the object, with special handling for null and arrays.
|
||||
@author Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
*/
|
||||
cint.typeOf(value)
|
||||
|
||||
/** Create a new instance of the given constructor with the given constructor arguments. Useful for higher order programmer where the new keyword won't work. */
|
||||
cint.createNew(C, args)
|
||||
|
||||
/** Converts the given value to a string by calling its toString method. */
|
||||
cint.intoString(value)
|
24
node_modules/cint/bower.json
generated
vendored
Normal file
24
node_modules/cint/bower.json
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"name": "cint",
|
||||
"version": "6.0.0",
|
||||
"homepage": "https://github.com/metaraine/cint",
|
||||
"authors": [
|
||||
"metaraine <raineorshine@gmail.com>"
|
||||
],
|
||||
"description": "A Javascript utility belt with a focus on Functional Programming.",
|
||||
"main": "cint.js",
|
||||
"keywords": [
|
||||
"functional",
|
||||
"utility",
|
||||
"underscore",
|
||||
"lodash"
|
||||
],
|
||||
"license": "ISC",
|
||||
"ignore": [
|
||||
"**/.*",
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"test",
|
||||
"tests"
|
||||
]
|
||||
}
|
893
node_modules/cint/cint.js
generated
vendored
Normal file
893
node_modules/cint/cint.js
generated
vendored
Normal file
|
@ -0,0 +1,893 @@
|
|||
/** A Javascript utility belt with an emphasis on Functional Programming.
|
||||
@module cint
|
||||
@author Raine Lourie
|
||||
@version v8.2.1 (Fri, 13 Feb 2015 20:24:14 GMT)
|
||||
*/
|
||||
cint = (function() {
|
||||
'use strict';
|
||||
|
||||
/***********************************
|
||||
* Private Functions
|
||||
***********************************/
|
||||
var _last = partialAt(index, 1, -1);
|
||||
|
||||
|
||||
|
||||
/***********************************
|
||||
* String
|
||||
***********************************/
|
||||
|
||||
/** Performs variable substitution on the string, replacing items in {curly braces}.
|
||||
Same as Lodash's _.template(str, o, { interpolate: /{([\s\S]+?)}/g }).
|
||||
Based on supplant by Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {Object|Array} o
|
||||
*/
|
||||
function supplant(str, o) {
|
||||
return str.replace(/{([^{}]*)}/g,
|
||||
function (a, b) {
|
||||
return b in o ? o[b] : a
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/** Returns true if the string starts with the given substring. Returns false if the substring is the empty string.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} sub The substring.
|
||||
*/
|
||||
function startsWith(str, sub){
|
||||
return str.indexOf(sub) === 0 && sub !== ''
|
||||
}
|
||||
|
||||
/** Returns the substring before the first instance of the given delimiter.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} delim
|
||||
*/
|
||||
function before(str, delim) {
|
||||
return str.split(delim)[0]
|
||||
}
|
||||
|
||||
/** Returns the substring after the first instance of the given delimiter. Returns the whole string if the delimiter is not found.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} delim
|
||||
*/
|
||||
function after(str, delim) {
|
||||
var delimIndex = str.indexOf(delim)
|
||||
return delimIndex >= 0 ?
|
||||
str.substring(delimIndex+delim.length) : str
|
||||
}
|
||||
|
||||
/** Returns the substring between the given delimiters.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} left
|
||||
@param {String} right
|
||||
*/
|
||||
function between(str, left, right) {
|
||||
return before(after(str, left), right)
|
||||
}
|
||||
|
||||
/** Wraps a string with a left and right. If right omitted, wraps both ends in left.
|
||||
@memberOf module:cint#
|
||||
@param {string} middle
|
||||
@param {string} left
|
||||
@param {string} right
|
||||
*/
|
||||
function bookend(middle, left, right) {
|
||||
return (left || '') + middle + (right || left || '')
|
||||
}
|
||||
|
||||
/** Returns a single string that repeats the string n times. Optionally joins it with the given delimeter
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {Number} n
|
||||
@param {String} delim Default: ''
|
||||
*/
|
||||
function repeatString(str, n, delim) {
|
||||
delim = delim || ''
|
||||
return mapNumber(n, function() { return str }).join(delim)
|
||||
}
|
||||
|
||||
/** Capitalizes the first letter of each word in the given string.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
*/
|
||||
function toTitleCase(str) {
|
||||
var capitalizeFirst = function(s) {
|
||||
return s.length ? s[0].toUpperCase() + s.substring(1).toLowerCase() : ''
|
||||
}
|
||||
return str.split(' ').map(capitalizeFirst).join(' ')
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Number
|
||||
***********************************/
|
||||
|
||||
/** Returns the ordinal value (like '1st' or '2nd') of the given integer.
|
||||
@memberOf module:cint#
|
||||
@param {Number} n
|
||||
*/
|
||||
function ordinal(n) {
|
||||
var lastDigit = n%10
|
||||
return n + (
|
||||
n >= 11 && n <= 13 ? 'th' :
|
||||
lastDigit === 1 ? 'st' :
|
||||
lastDigit === 2 ? 'nd' :
|
||||
lastDigit === 3 ? 'rd' :
|
||||
'th')
|
||||
}
|
||||
|
||||
/** Invokes the given function n times, passing the index for each invocation, and returns an array of the results.
|
||||
@memberOf module:cint#
|
||||
@param {Number} n
|
||||
@param {Function} f
|
||||
*/
|
||||
function mapNumber(n, f) {
|
||||
var results = []
|
||||
for(var i=0; i<n; i++) {
|
||||
results.push(f(i))
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
/** Adds two numbers. */
|
||||
function addTwo(x, y) {
|
||||
return x + y
|
||||
}
|
||||
|
||||
/** Adds all given arguments. */
|
||||
function add(/*x,y,z,...*/) {
|
||||
return arguments.length ? Array.prototype.reduce.call(arguments, addTwo) : 0
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Array
|
||||
***********************************/
|
||||
|
||||
/** Group the array of objects by one of the object's properties or mappable function. Returns an array of { key: ___, items: ___ } objects which represent all the items in the original array grouped by the value of the specified grouping key.
|
||||
@memberOf module:cint#
|
||||
@param {Object[]} arr
|
||||
@param {String|Function} propOrFunc
|
||||
*/
|
||||
function orderedGroup(arr, propOrFunc) {
|
||||
|
||||
if(!propOrFunc) {
|
||||
throw new Error('You must specific a property name or mappable function.')
|
||||
}
|
||||
|
||||
var getGroupKey = typeof propOrFunc === 'function' ?
|
||||
propOrFunc :
|
||||
function(item) { return item[propOrFunc]; }
|
||||
|
||||
var results = []
|
||||
var dict = {}
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var key = getGroupKey(arr[i])
|
||||
if(!(key in dict)) {
|
||||
dict[key] = []
|
||||
results.push({key: key, items: dict[key]})
|
||||
}
|
||||
dict[key].push(arr[i])
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
/** Returns a dictionary whose keys are the values of the array and values are the number of instances of that value within the array.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
*/
|
||||
function tally(arr) {
|
||||
var dict = {}
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var count = dict[arr[i]] || 0
|
||||
dict[arr[i]] = count + 1
|
||||
}
|
||||
return dict
|
||||
}
|
||||
|
||||
/** Tally the property values of an array of object, grouping the counts for each property under its value.
|
||||
e.g.
|
||||
[
|
||||
{
|
||||
ideal: 4,
|
||||
past: 3,
|
||||
present: 7
|
||||
},
|
||||
{
|
||||
ideal: 5,
|
||||
past: 7,
|
||||
present: 7
|
||||
}
|
||||
]
|
||||
|
||||
=>
|
||||
|
||||
{
|
||||
"4": {
|
||||
ideal: 1
|
||||
},
|
||||
"3": {
|
||||
past: 1
|
||||
}
|
||||
"5": {
|
||||
ideal: 1
|
||||
}
|
||||
"7": {
|
||||
present: 2,
|
||||
past: 1
|
||||
}
|
||||
}
|
||||
*/
|
||||
function tallyProps(arr) {
|
||||
|
||||
var tallies = {}
|
||||
|
||||
for(var i=0; i<arr.length; i++) {
|
||||
|
||||
var o = arr[i]
|
||||
|
||||
// loop through each property so the value can be tallied in the tallies object
|
||||
for(var key in o) {
|
||||
|
||||
// cache the value
|
||||
var value = o[key];
|
||||
|
||||
// create a new tally object if it doesn't exist for this value
|
||||
if(!tallies[value]) {
|
||||
tallies[value] = {}
|
||||
}
|
||||
|
||||
// increment the count
|
||||
tallies[value][key] = (tallies[value][key] || 0) + 1
|
||||
}
|
||||
}
|
||||
|
||||
return tallies
|
||||
}
|
||||
|
||||
|
||||
/** Returns the in-bounds index of the given index for the array, supports negative and out-of-bounds indices.
|
||||
@private
|
||||
@param {Array} arr
|
||||
@param {Number} i
|
||||
*/
|
||||
function circ(arr, i) {
|
||||
|
||||
// return first index if i is null or undefined
|
||||
if(i === undefined || i === null) {
|
||||
return arr[0]
|
||||
}
|
||||
|
||||
// one modulus to get in range, another to eliminate negative
|
||||
return (i % arr.length + arr.length) % arr.length
|
||||
}
|
||||
|
||||
/** Indexes into an array, supports negative indices.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} n
|
||||
*/
|
||||
function index(arr, i) {
|
||||
return arr[circ(arr, i)]
|
||||
}
|
||||
|
||||
/** Returns a new array containing the elements of the given array shifted n spaces to the left, wrapping around the end.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} n
|
||||
*/
|
||||
function rotate(arr, n) {
|
||||
var output = []
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
output.push(index(arr, i+n))
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
/** Creates an object with a property for each element of the given array, determined by a function that returns the property as a { key: value }.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Function} f
|
||||
*/
|
||||
function toObject(arr, f) {
|
||||
var keyValues = []
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
keyValues.push(f(arr[i], i))
|
||||
}
|
||||
return merge.apply(arr, keyValues)
|
||||
}
|
||||
|
||||
/** Functional, nondestructive version of Array.prototype.splice.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} index
|
||||
@param {Number} howMany
|
||||
@param {...*} elements
|
||||
*/
|
||||
function spliced(arr, index, howMany/*, elements*/) {
|
||||
var elements = Array.prototype.slice.apply(arguments, [3])
|
||||
var elementsLen = elements.length
|
||||
var results = []
|
||||
var len = arr.length
|
||||
|
||||
// add starting elements
|
||||
for(var i=0; i<index && i<len; i++) {
|
||||
results.push(arr[i])
|
||||
}
|
||||
|
||||
// add inserted elements
|
||||
for(i=0; i<elementsLen; i++) {
|
||||
results.push(elements[i])
|
||||
}
|
||||
|
||||
// add ending elements
|
||||
for(i=index+howMany; i<len; i++) {
|
||||
results.push(arr[i])
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
/** Breaks up the array into n evenly-sized chunks.
|
||||
Solution from http://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} n
|
||||
*/
|
||||
function chunk(a, n) {
|
||||
var len = a.length,out = [], i = 0
|
||||
while (i < len) {
|
||||
var size = Math.ceil((len - i) / n--)
|
||||
out.push(a.slice(i, i += size))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Object
|
||||
***********************************/
|
||||
|
||||
/** Returns a new object with the given key and value.
|
||||
@memberOf module:cint#
|
||||
@param {String} key
|
||||
@param value
|
||||
*/
|
||||
function keyValue(key, value) {
|
||||
var o = {}
|
||||
o[key] = value
|
||||
return o
|
||||
}
|
||||
|
||||
/** Gets the value of a key of the given object.
|
||||
*/
|
||||
function getValue(o, key) {
|
||||
return o[key];
|
||||
}
|
||||
|
||||
/** Sets the value of the given key and returns the object.
|
||||
*/
|
||||
function setValue(o, key, value) {
|
||||
o[key] = value
|
||||
return o
|
||||
}
|
||||
|
||||
/** Creates a mapping function that applies the given function to the value of the specified key.
|
||||
*/
|
||||
function mapOverKey(f, originalKey, newKey) {
|
||||
return function(o) {
|
||||
return setValue(o, newKey || originalKey, f(o[originalKey]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Join the object into a single string with the given separators separating properties from each other as well as values.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {String} propSeparator
|
||||
@param {String} valueSeparator
|
||||
*/
|
||||
function joinObject(obj, propSeparator, valueSeparator) {
|
||||
var keyValuePairs = []
|
||||
for(var prop in obj) {
|
||||
keyValuePairs.push(prop + valueSeparator + obj[prop])
|
||||
}
|
||||
return keyValuePairs.join(propSeparator)
|
||||
}
|
||||
|
||||
/** Returns a new object with the given objects merged onto it. Non-undefined properties on later arguments override identical properties on earlier arguments.
|
||||
@memberOf module:cint#
|
||||
@param {...Object} objects
|
||||
*/
|
||||
function merge(/*obj1, obj2, obj3, ...*/) {
|
||||
|
||||
var mothership = {}
|
||||
|
||||
// iterate through each given object
|
||||
var len = arguments.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var outlier = arguments[i]
|
||||
|
||||
// add each property to the mothership
|
||||
for(var prop in outlier) {
|
||||
if(typeOf(outlier[prop]) === 'object' && outlier[prop].constructor === Object && outlier[prop] !== null && !(outlier[prop] instanceof Array)) {
|
||||
mothership[prop] = merge(mothership[prop], outlier[prop]) // RECURSION
|
||||
}
|
||||
else if(outlier[prop] !== undefined) {
|
||||
mothership[prop] = outlier[prop]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mothership
|
||||
}
|
||||
|
||||
/** Returns a new object where f(key, value) returns a new key-value pair for each property.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {Function} f
|
||||
*/
|
||||
function mapObject(obj, f) {
|
||||
var result = {}
|
||||
for(var key in obj) {
|
||||
var pair = f(key, obj[key])
|
||||
for(var prop in pair) {
|
||||
result[prop] = pair[prop]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns an array whose items are the result of calling f(key, value) on each property of the given object. If f is undefined, returns a list of { key: ___, value: ___ } objects.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {Function} f
|
||||
*/
|
||||
function toArray(obj, f) {
|
||||
f = f || function(key, value) { return { key: key, value: value } }
|
||||
var result = []
|
||||
for(var key in obj) {
|
||||
result.push(f(key, obj[key]))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a new object that only includes the properties of the given obj for which f(key, value) is true.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {Function} f
|
||||
*/
|
||||
function filterObject(obj, f) {
|
||||
var result = {}
|
||||
for(var key in obj) {
|
||||
if(f(key, obj[key])) {
|
||||
result[key] = obj[key]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Changes the specified keys in an object.
|
||||
@param {Object} obj
|
||||
@param {Object} changeKeys
|
||||
@example cint.changeKeys(
|
||||
{ fname: 'Raine', lname: 'Lourie', specialty: 'Javascript' },
|
||||
{ fname: 'first', lname: 'last' }
|
||||
)
|
||||
*/
|
||||
function changeKeys(obj, changedKeys) {
|
||||
var result = {}
|
||||
for(var key in obj) {
|
||||
result[key in changedKeys ? changedKeys[key] : key] = obj[key]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Calls a function on an object and returns the object (for chaining purposes).
|
||||
*/
|
||||
function tap(f, o) {
|
||||
f(o)
|
||||
return o
|
||||
}
|
||||
|
||||
/* console.log's the given object and returns the object (for chaining purposes).
|
||||
*/
|
||||
function look(o) {
|
||||
console.log(o)
|
||||
return o
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Function
|
||||
***********************************/
|
||||
|
||||
/** Returns a function that returns the inverse of the given boolean function.
|
||||
@memberOf module:cint#
|
||||
@param {function} f The function to inverse.
|
||||
*/
|
||||
function not(f) {
|
||||
return function() {
|
||||
return !f.apply(this, arguments)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a new function that inserts the given curried arguments to the inner function at the specified index of its runtime arguments.
|
||||
@memberOf module:cint#
|
||||
@example _.partial(f, args...) is equivalent to cint.partialAt(f, 0, args) and _.partialRight(f, args...) is equivalent to cint.partialAt(f, n, args) where n is the arity of the function.
|
||||
@param {Function} f
|
||||
@param {Number} index
|
||||
@param {...*} curriedArgs
|
||||
*/
|
||||
function partialAt(f, index, curriedArgs) {
|
||||
return function() {
|
||||
var givenArgs = Array.prototype.slice.call(arguments)
|
||||
|
||||
// handle negative indices
|
||||
// Note that we add 1 so that -1 points to the slot AFTER the last element, not before the last element (which is what the last index points to).
|
||||
if(index < 0) {
|
||||
index = circ(givenArgs, index) + 1
|
||||
}
|
||||
|
||||
var spliceArgs = [givenArgs, index, 0].concat(curriedArgs)
|
||||
var newArgs = spliced.apply(this, spliceArgs)
|
||||
return f.apply(this, newArgs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a new function that calls the given function with a limit on the number of arguments.
|
||||
@memberOf module:cint#
|
||||
@param {Function} f
|
||||
@param {Number} n
|
||||
*/
|
||||
function aritize(f, n) {
|
||||
return function() {
|
||||
var givenArgs = Array.prototype.slice.call(arguments, 0, n)
|
||||
return f.apply(this, givenArgs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Recursively invokes the given function with no parameters until it returns a non-functional value.
|
||||
@memberOf module:cint#
|
||||
@param value
|
||||
*/
|
||||
function callTillValue(value) {
|
||||
return typeof value === 'function' ? callTillValue(value()) : value
|
||||
}
|
||||
|
||||
/** Returns a function that calls the given function as normal, then passes its inputs and output to the spier (defaults to console.log)
|
||||
@memberOf module:cint#
|
||||
@param {Function} f
|
||||
@param {Function} [spier=console.log]
|
||||
*/
|
||||
function spy(f, spier) {
|
||||
var that = this
|
||||
/* jshint ignore:start */
|
||||
spier = spier || console.log.bind(console)
|
||||
/* jshint ignore:end */
|
||||
|
||||
return function() {
|
||||
var args = Array.prototype.slice.call(arguments)
|
||||
var out = f.apply(that, args)
|
||||
spier.call(that, f, args, out)
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a copy of the given function that calls the original function in the context of the first argument. Passes arguments 1..n as normal.
|
||||
@memberOf module:cint#
|
||||
@param f
|
||||
*/
|
||||
function inContext(f) {
|
||||
return function(context) {
|
||||
var otherArgs = Array.prototype.slice.call(arguments, 1)
|
||||
return f.apply(context, otherArgs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Converts the given synchronous function into an asynchronous function that applies its arguments to the original function and invokes callback(error, result).
|
||||
@memberOf module:cint#
|
||||
@param f
|
||||
*/
|
||||
function toAsync(f) {
|
||||
return function(/* [arg1], [arg2], ..., callback */) {
|
||||
var that = this;
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var callback = _last(args);
|
||||
setTimeout(function() {
|
||||
try {
|
||||
var result = f.apply(that, args);
|
||||
callback(null, result);
|
||||
}
|
||||
catch(e) {
|
||||
callback(e);
|
||||
}
|
||||
}, 0)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***********************************
|
||||
* Utility
|
||||
***********************************/
|
||||
|
||||
/** Compares two items lexigraphically. Returns 1 if a>b, 0 if a==b, or -1 if a<b.
|
||||
@memberOf module:cint#
|
||||
@param a
|
||||
@param b
|
||||
*/
|
||||
function compare(a,b) {
|
||||
return a > b ? 1 :
|
||||
a < b ? -1 :
|
||||
0
|
||||
}
|
||||
|
||||
/** Compares the value of the given property between two objects.
|
||||
@memberOf module:cint#
|
||||
@param {String} property
|
||||
*/
|
||||
function compareProperty(property, a, b) {
|
||||
return compare(a[property], b[property])
|
||||
}
|
||||
|
||||
/** Returns a compare function that can be passed to Array.sort to sort in the order of the given array of properties. A property can also be appended with ' ASC' or ' DESC' to control the sort order.
|
||||
@memberOf module:cint#
|
||||
@param {String[]|Object[]} props
|
||||
*/
|
||||
function dynamicCompare(props) {
|
||||
|
||||
if(!props || !(props instanceof Array)) {
|
||||
throw new Error('props is falsey or not an Array')
|
||||
}
|
||||
|
||||
return function(a,b) {
|
||||
var len = props.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var aVal, bVal, sortDir
|
||||
if(typeof props[i] == 'function') {
|
||||
aVal = props[i](a)
|
||||
bVal = props[i](b)
|
||||
sortDir = 'asc'
|
||||
}
|
||||
|
||||
else if(props[i].toLowerCase().indexOf(' ') >= 0) {
|
||||
var splitVal = props[i].split(' ')
|
||||
aVal = a[splitVal[0]]
|
||||
bVal = b[splitVal[0]]
|
||||
sortDir = splitVal[1].toLowerCase()
|
||||
}
|
||||
else {
|
||||
aVal = a[props[i]]
|
||||
bVal = b[props[i]]
|
||||
sortDir = 'asc'
|
||||
}
|
||||
|
||||
// this is important so that if the values are equial, it continues to the next sort property
|
||||
if(aVal != bVal) {
|
||||
return sortDir == 'asc' ? compare(aVal,bVal) : compare(bVal,aVal)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true if all the items in a are equal to all the items in b, recursively.
|
||||
@memberOf module:cint#
|
||||
@param a
|
||||
@param b
|
||||
*/
|
||||
function equals(a,b) {
|
||||
|
||||
if(typeof a !== typeof b) {
|
||||
return false
|
||||
}
|
||||
|
||||
// compare arrays
|
||||
if(a instanceof Array) {
|
||||
|
||||
// check if the arrays are the same length
|
||||
if(a.length !== b.length) {
|
||||
return false
|
||||
}
|
||||
|
||||
// check the equality of each item
|
||||
for(var i=0, l=a.length; i<l; i++) {
|
||||
if(!b || !b[i] || !equals(a[i], b[i])) { // RECURSION
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
// compare primitives
|
||||
else if(typeof a === 'number' || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'undefined') {
|
||||
if(a !== b) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
// compare objects
|
||||
else if(Object.keys(a).length === Object.keys(b)) {
|
||||
for(var property in a) {
|
||||
if(!(property in b && b[property] === a[property])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return a === b
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/** Returns true if the given value is not undefined, null, or an empty string.
|
||||
@memberOf module:cint#
|
||||
@param x
|
||||
*/
|
||||
function isValue(x) {
|
||||
return x !== undefined && x !== null && x !== ''
|
||||
}
|
||||
|
||||
/** Returns a string representation of the given scalar, array, or dictionary.
|
||||
@memberOf module:cint#
|
||||
@param o
|
||||
*/
|
||||
function hash(o) {
|
||||
if(o === undefined) {
|
||||
return 'undefined'
|
||||
}
|
||||
else if(o === null) {
|
||||
return 'null'
|
||||
}
|
||||
else if(typeof o === 'string' || typeof o === 'number') {
|
||||
return '' + o
|
||||
}
|
||||
else if(typeOf(o) === 'array') {
|
||||
return '_[{0}]_'.format(o.map(hash).join(','))
|
||||
}
|
||||
else if(typeOf(o) === 'object') {
|
||||
var objString = ''
|
||||
for(var prop in o) {
|
||||
objString += supplant('{0}_:_{1}', [prop, hash(o[prop])])
|
||||
}
|
||||
// escape for handlebars
|
||||
return supplant('_\{{0}}_', [objString]) // jshint ignore:line
|
||||
}
|
||||
else {
|
||||
throw new Error('Unhashable value: ' + o)
|
||||
}
|
||||
}
|
||||
|
||||
/** Generates a pseudo-random string that can be assumed to be unique. */
|
||||
var guid = (function() {
|
||||
var S4 = function() {
|
||||
return (((1+Math.random())*0x10000)|0).toString(16).substring(1)
|
||||
}
|
||||
return function() {
|
||||
return S4()+S4()+'-'+S4()+'-'+S4()+'-'+S4()+'-'+S4()+S4()+S4()
|
||||
}
|
||||
})()
|
||||
|
||||
/** Returns a string representing the type of the object, with special handling for null and arrays.
|
||||
@author Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
@param value
|
||||
*/
|
||||
function typeOf(value) {
|
||||
var s = typeof value
|
||||
if (s === 'object') {
|
||||
if (value) {
|
||||
if (typeof value.length === 'number' &&
|
||||
!(value.propertyIsEnumerable('length')) &&
|
||||
typeof value.splice === 'function') {
|
||||
s = 'array'
|
||||
}
|
||||
} else {
|
||||
s = 'null'
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
/** Create a new instance of the given constructor with the given constructor arguments. Useful for higher order programmer where the new keyword won't work.
|
||||
@memberOf module:cint#
|
||||
@param {Function} C Constructor
|
||||
@param {Array} args
|
||||
*/
|
||||
function createNew(C, args) {
|
||||
var o = new C()
|
||||
C.apply(o, args)
|
||||
return o
|
||||
}
|
||||
|
||||
|
||||
/** Converts the given value to a string by calling its toString method.
|
||||
*/
|
||||
function intoString(value) {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***********************************
|
||||
* Export Public Interface
|
||||
***********************************/
|
||||
|
||||
return {
|
||||
|
||||
// string
|
||||
supplant : supplant,
|
||||
startsWith : startsWith,
|
||||
before : before,
|
||||
after : after,
|
||||
between : between,
|
||||
bookend : bookend,
|
||||
repeatString : repeatString,
|
||||
toTitleCase : toTitleCase,
|
||||
|
||||
// number
|
||||
ordinal : ordinal,
|
||||
mapNumber : mapNumber,
|
||||
addTwo : addTwo,
|
||||
add : add,
|
||||
|
||||
// array
|
||||
orderedGroup : orderedGroup,
|
||||
tally : tally,
|
||||
tallyProps : tallyProps,
|
||||
index : index,
|
||||
rotate : rotate,
|
||||
toObject : toObject,
|
||||
spliced : spliced,
|
||||
chunk : chunk,
|
||||
|
||||
// object
|
||||
keyValue : keyValue,
|
||||
getValue : getValue,
|
||||
setValue : setValue,
|
||||
mapOverKey : mapOverKey,
|
||||
joinObject : joinObject,
|
||||
mapObject : mapObject,
|
||||
toArray : toArray,
|
||||
filterObject : filterObject,
|
||||
changeKeys : changeKeys,
|
||||
tap : tap,
|
||||
look : look,
|
||||
|
||||
// function
|
||||
not : not,
|
||||
partialAt : partialAt,
|
||||
aritize : aritize,
|
||||
callTillValue : callTillValue,
|
||||
spy : spy,
|
||||
inContext : inContext,
|
||||
toAsync : toAsync,
|
||||
|
||||
// utility
|
||||
compare : compare,
|
||||
compareProperty : compareProperty,
|
||||
dynamicCompare : dynamicCompare,
|
||||
equals : equals,
|
||||
isValue : isValue,
|
||||
hash : hash,
|
||||
guid : guid,
|
||||
typeOf : typeOf,
|
||||
'new' : createNew,
|
||||
intoString : intoString
|
||||
}
|
||||
|
||||
})()
|
||||
|
||||
// requirejs export
|
||||
if(typeof module !== 'undefined') {
|
||||
module.exports = cint
|
||||
}
|
6
node_modules/cint/cint.min.js
generated
vendored
Normal file
6
node_modules/cint/cint.min.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
28
node_modules/cint/experimental/experimental.js
generated
vendored
Normal file
28
node_modules/cint/experimental/experimental.js
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
/** Other functional functions I'm playing around with */
|
||||
|
||||
/** Returns a function with a clean prototype that calls the base function. */
|
||||
Function.prototype.copy = function() {
|
||||
var f = this;
|
||||
return function() {
|
||||
return f.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a constructor that will take an object as an initializer list. */
|
||||
function initializer() {
|
||||
return function(members) {
|
||||
for(key in members) {
|
||||
this[key] = members[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a constructor that will take an object as an initializer list. Takes
|
||||
an object of properties that are added to the constructor's prototype. */
|
||||
function protoInitializer(protoMembers) {
|
||||
var construct = initializer();
|
||||
for(key in protoMembers) {
|
||||
construct.prototype[key] = protoMembers[key];
|
||||
}
|
||||
return construct;
|
||||
}
|
68
node_modules/cint/experimental/rjformat.js
generated
vendored
Normal file
68
node_modules/cint/experimental/rjformat.js
generated
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
var RJFormat = (function() {
|
||||
|
||||
/*********************
|
||||
* PRIVATE
|
||||
*********************/
|
||||
|
||||
/** The string to use if the raw value is not valid. */
|
||||
var invalidValueStandin = "--";
|
||||
|
||||
/** Returns true if the given value is not null or undefined. */
|
||||
var isValue = function(val) {
|
||||
return val !== undefined && val !== null;
|
||||
};
|
||||
|
||||
/** Generates a function that calls a formatter function if the given value is valid, otherwise returns the invalidValueStandin. */
|
||||
var validOr = function(f) {
|
||||
return function(val) {
|
||||
return isValue(val) ? f(val) : invalidValueStandin;
|
||||
};
|
||||
};
|
||||
|
||||
/*********************
|
||||
* PUBLIC
|
||||
*********************/
|
||||
|
||||
/** Mark thousands with commas. 4000000 -> 4,000,000. */
|
||||
var commatize = function(val) {
|
||||
var valString = val.toString();
|
||||
var output = [];
|
||||
for(var i=valString.length-1; i>=0; i--) {
|
||||
var fromRight = valString.length - i - 1;
|
||||
if(fromRight !== 0 && fromRight%3 === 0) {
|
||||
output.unshift(",");
|
||||
}
|
||||
output.unshift(valString[i]);
|
||||
}
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
/** Formats the given value as a percentage. Returns the invalidValueStandin if the given raw value is null or undefined. */
|
||||
var percentage = validOr(function(val) {
|
||||
return val * 100 + "%";
|
||||
});
|
||||
|
||||
var currency = validOr(function(val) {
|
||||
|
||||
var valString = val.toString();
|
||||
var decimelLoc = valString.indexOf(".");
|
||||
var decimelPlaces = decimelLoc === -1 ? 0 :
|
||||
valString.length - valString.indexOf(".") - 1;
|
||||
|
||||
var mixed = valString.split(".");
|
||||
var whole = mixed[0];
|
||||
var fraction = mixed.length > 1 ? mixed[1].substring(0,2) : "";
|
||||
|
||||
return "${0}.{1}".supplant(
|
||||
commatize(whole),
|
||||
fraction + "0".repeat(2 - fraction.length)
|
||||
);
|
||||
});
|
||||
|
||||
// public interface
|
||||
return {
|
||||
commatize: commatize,
|
||||
currency: currency,
|
||||
percentage: percentage
|
||||
};
|
||||
})();
|
41
node_modules/cint/gulpfile.js
generated
vendored
Normal file
41
node_modules/cint/gulpfile.js
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
var gulp = require('gulp'),
|
||||
fs = require('fs'),
|
||||
pkg = require('./package.json'),
|
||||
open = require('gulp-open'),
|
||||
jshint = require('gulp-jshint'),
|
||||
clean = require('gulp-clean'),
|
||||
uglify = require('gulp-uglify'),
|
||||
rename = require('gulp-rename'),
|
||||
header = require('gulp-header'),
|
||||
extend = require('lodash.assign');
|
||||
|
||||
var srcPath = 'src/cint.js',
|
||||
destPath = './',
|
||||
docsPath = 'docs';
|
||||
|
||||
gulp.task('default', ['clean'], function() {
|
||||
|
||||
var buildPackage = extend(pkg, { buildtime: (new Date()).toUTCString() });
|
||||
var headerTemplate = fs.readFileSync('header.ejs');
|
||||
|
||||
gulp.src(srcPath)
|
||||
.pipe(jshint('.jshintrc'))
|
||||
.pipe(jshint.reporter('jshint-stylish'))
|
||||
.pipe(jshint.reporter('fail'))
|
||||
.pipe(header(headerTemplate, buildPackage))
|
||||
.pipe(gulp.dest(destPath))
|
||||
.pipe(uglify())
|
||||
.pipe(header(headerTemplate, buildPackage))
|
||||
.pipe(rename({ suffix: '.min' }))
|
||||
.pipe(gulp.dest(destPath))
|
||||
});
|
||||
|
||||
gulp.task('clean', function() {
|
||||
gulp.src(['docs'], {read: false})
|
||||
.pipe(clean({force:true}));
|
||||
});
|
||||
|
||||
})();
|
5
node_modules/cint/header.ejs
generated
vendored
Normal file
5
node_modules/cint/header.ejs
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
/** A Javascript utility belt with an emphasis on Functional Programming.
|
||||
@module <%= name %>
|
||||
@author <%= author %>
|
||||
@version v<%= version %> (<%= buildtime %>)
|
||||
*/
|
70
node_modules/cint/package.json
generated
vendored
Normal file
70
node_modules/cint/package.json
generated
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
{
|
||||
"_from": "cint@^8.2.1",
|
||||
"_id": "cint@8.2.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-cDhrG0jidz0NYxZqVa/5TvRFahI=",
|
||||
"_location": "/cint",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "cint@^8.2.1",
|
||||
"name": "cint",
|
||||
"escapedName": "cint",
|
||||
"rawSpec": "^8.2.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^8.2.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/npm-check-updates"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/cint/-/cint-8.2.1.tgz",
|
||||
"_shasum": "70386b1b48e2773d0d63166a55aff94ef4456a12",
|
||||
"_spec": "cint@^8.2.1",
|
||||
"_where": "/home/shimataro/projects/actions/ssh-key-action/node_modules/npm-check-updates",
|
||||
"author": {
|
||||
"name": "Raine Lourie"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/metaraine/cint/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {},
|
||||
"deprecated": false,
|
||||
"description": "A library of Javascript utility functions with an emphasis on Functional Programming.",
|
||||
"devDependencies": {
|
||||
"chai": "^2.0.0",
|
||||
"gulp": "^3.5.2",
|
||||
"gulp-clean": "^0.2.4",
|
||||
"gulp-header": "^1.0.2",
|
||||
"gulp-jshint": "^1.4.2",
|
||||
"gulp-open": "^0.2.8",
|
||||
"gulp-rename": "^1.1.0",
|
||||
"gulp-template": "^0.1.1",
|
||||
"gulp-uglify": "^0.2.1",
|
||||
"gulp-util": "^2.2.14",
|
||||
"jshint-stylish": "^0.1.5",
|
||||
"lodash.assign": "^2.4.1",
|
||||
"mocha": "^2.1.0"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"gitHead": "ba9f4a85fd605fdad2bde942d59b57c7415bd709",
|
||||
"homepage": "https://github.com/metaraine/cint#readme",
|
||||
"keywords": [
|
||||
"functional",
|
||||
"utility"
|
||||
],
|
||||
"license": "ISC",
|
||||
"main": "cint.js",
|
||||
"name": "cint",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/metaraine/cint.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"version": "8.2.1"
|
||||
}
|
888
node_modules/cint/src/cint.js
generated
vendored
Normal file
888
node_modules/cint/src/cint.js
generated
vendored
Normal file
|
@ -0,0 +1,888 @@
|
|||
cint = (function() {
|
||||
'use strict';
|
||||
|
||||
/***********************************
|
||||
* Private Functions
|
||||
***********************************/
|
||||
var _last = partialAt(index, 1, -1);
|
||||
|
||||
|
||||
|
||||
/***********************************
|
||||
* String
|
||||
***********************************/
|
||||
|
||||
/** Performs variable substitution on the string, replacing items in {curly braces}.
|
||||
Same as Lodash's _.template(str, o, { interpolate: /{([\s\S]+?)}/g }).
|
||||
Based on supplant by Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {Object|Array} o
|
||||
*/
|
||||
function supplant(str, o) {
|
||||
return str.replace(/{([^{}]*)}/g,
|
||||
function (a, b) {
|
||||
return b in o ? o[b] : a
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/** Returns true if the string starts with the given substring. Returns false if the substring is the empty string.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} sub The substring.
|
||||
*/
|
||||
function startsWith(str, sub){
|
||||
return str.indexOf(sub) === 0 && sub !== ''
|
||||
}
|
||||
|
||||
/** Returns the substring before the first instance of the given delimiter.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} delim
|
||||
*/
|
||||
function before(str, delim) {
|
||||
return str.split(delim)[0]
|
||||
}
|
||||
|
||||
/** Returns the substring after the first instance of the given delimiter. Returns the whole string if the delimiter is not found.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} delim
|
||||
*/
|
||||
function after(str, delim) {
|
||||
var delimIndex = str.indexOf(delim)
|
||||
return delimIndex >= 0 ?
|
||||
str.substring(delimIndex+delim.length) : str
|
||||
}
|
||||
|
||||
/** Returns the substring between the given delimiters.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {String} left
|
||||
@param {String} right
|
||||
*/
|
||||
function between(str, left, right) {
|
||||
return before(after(str, left), right)
|
||||
}
|
||||
|
||||
/** Wraps a string with a left and right. If right omitted, wraps both ends in left.
|
||||
@memberOf module:cint#
|
||||
@param {string} middle
|
||||
@param {string} left
|
||||
@param {string} right
|
||||
*/
|
||||
function bookend(middle, left, right) {
|
||||
return (left || '') + middle + (right || left || '')
|
||||
}
|
||||
|
||||
/** Returns a single string that repeats the string n times. Optionally joins it with the given delimeter
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
@param {Number} n
|
||||
@param {String} delim Default: ''
|
||||
*/
|
||||
function repeatString(str, n, delim) {
|
||||
delim = delim || ''
|
||||
return mapNumber(n, function() { return str }).join(delim)
|
||||
}
|
||||
|
||||
/** Capitalizes the first letter of each word in the given string.
|
||||
@memberOf module:cint#
|
||||
@param {String} str
|
||||
*/
|
||||
function toTitleCase(str) {
|
||||
var capitalizeFirst = function(s) {
|
||||
return s.length ? s[0].toUpperCase() + s.substring(1).toLowerCase() : ''
|
||||
}
|
||||
return str.split(' ').map(capitalizeFirst).join(' ')
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Number
|
||||
***********************************/
|
||||
|
||||
/** Returns the ordinal value (like '1st' or '2nd') of the given integer.
|
||||
@memberOf module:cint#
|
||||
@param {Number} n
|
||||
*/
|
||||
function ordinal(n) {
|
||||
var lastDigit = n%10
|
||||
return n + (
|
||||
n >= 11 && n <= 13 ? 'th' :
|
||||
lastDigit === 1 ? 'st' :
|
||||
lastDigit === 2 ? 'nd' :
|
||||
lastDigit === 3 ? 'rd' :
|
||||
'th')
|
||||
}
|
||||
|
||||
/** Invokes the given function n times, passing the index for each invocation, and returns an array of the results.
|
||||
@memberOf module:cint#
|
||||
@param {Number} n
|
||||
@param {Function} f
|
||||
*/
|
||||
function mapNumber(n, f) {
|
||||
var results = []
|
||||
for(var i=0; i<n; i++) {
|
||||
results.push(f(i))
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
/** Adds two numbers. */
|
||||
function addTwo(x, y) {
|
||||
return x + y
|
||||
}
|
||||
|
||||
/** Adds all given arguments. */
|
||||
function add(/*x,y,z,...*/) {
|
||||
return arguments.length ? Array.prototype.reduce.call(arguments, addTwo) : 0
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Array
|
||||
***********************************/
|
||||
|
||||
/** Group the array of objects by one of the object's properties or mappable function. Returns an array of { key: ___, items: ___ } objects which represent all the items in the original array grouped by the value of the specified grouping key.
|
||||
@memberOf module:cint#
|
||||
@param {Object[]} arr
|
||||
@param {String|Function} propOrFunc
|
||||
*/
|
||||
function orderedGroup(arr, propOrFunc) {
|
||||
|
||||
if(!propOrFunc) {
|
||||
throw new Error('You must specific a property name or mappable function.')
|
||||
}
|
||||
|
||||
var getGroupKey = typeof propOrFunc === 'function' ?
|
||||
propOrFunc :
|
||||
function(item) { return item[propOrFunc]; }
|
||||
|
||||
var results = []
|
||||
var dict = {}
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var key = getGroupKey(arr[i])
|
||||
if(!(key in dict)) {
|
||||
dict[key] = []
|
||||
results.push({key: key, items: dict[key]})
|
||||
}
|
||||
dict[key].push(arr[i])
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
/** Returns a dictionary whose keys are the values of the array and values are the number of instances of that value within the array.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
*/
|
||||
function tally(arr) {
|
||||
var dict = {}
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var count = dict[arr[i]] || 0
|
||||
dict[arr[i]] = count + 1
|
||||
}
|
||||
return dict
|
||||
}
|
||||
|
||||
/** Tally the property values of an array of object, grouping the counts for each property under its value.
|
||||
e.g.
|
||||
[
|
||||
{
|
||||
ideal: 4,
|
||||
past: 3,
|
||||
present: 7
|
||||
},
|
||||
{
|
||||
ideal: 5,
|
||||
past: 7,
|
||||
present: 7
|
||||
}
|
||||
]
|
||||
|
||||
=>
|
||||
|
||||
{
|
||||
"4": {
|
||||
ideal: 1
|
||||
},
|
||||
"3": {
|
||||
past: 1
|
||||
}
|
||||
"5": {
|
||||
ideal: 1
|
||||
}
|
||||
"7": {
|
||||
present: 2,
|
||||
past: 1
|
||||
}
|
||||
}
|
||||
*/
|
||||
function tallyProps(arr) {
|
||||
|
||||
var tallies = {}
|
||||
|
||||
for(var i=0; i<arr.length; i++) {
|
||||
|
||||
var o = arr[i]
|
||||
|
||||
// loop through each property so the value can be tallied in the tallies object
|
||||
for(var key in o) {
|
||||
|
||||
// cache the value
|
||||
var value = o[key];
|
||||
|
||||
// create a new tally object if it doesn't exist for this value
|
||||
if(!tallies[value]) {
|
||||
tallies[value] = {}
|
||||
}
|
||||
|
||||
// increment the count
|
||||
tallies[value][key] = (tallies[value][key] || 0) + 1
|
||||
}
|
||||
}
|
||||
|
||||
return tallies
|
||||
}
|
||||
|
||||
|
||||
/** Returns the in-bounds index of the given index for the array, supports negative and out-of-bounds indices.
|
||||
@private
|
||||
@param {Array} arr
|
||||
@param {Number} i
|
||||
*/
|
||||
function circ(arr, i) {
|
||||
|
||||
// return first index if i is null or undefined
|
||||
if(i === undefined || i === null) {
|
||||
return arr[0]
|
||||
}
|
||||
|
||||
// one modulus to get in range, another to eliminate negative
|
||||
return (i % arr.length + arr.length) % arr.length
|
||||
}
|
||||
|
||||
/** Indexes into an array, supports negative indices.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} n
|
||||
*/
|
||||
function index(arr, i) {
|
||||
return arr[circ(arr, i)]
|
||||
}
|
||||
|
||||
/** Returns a new array containing the elements of the given array shifted n spaces to the left, wrapping around the end.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} n
|
||||
*/
|
||||
function rotate(arr, n) {
|
||||
var output = []
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
output.push(index(arr, i+n))
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
/** Creates an object with a property for each element of the given array, determined by a function that returns the property as a { key: value }.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Function} f
|
||||
*/
|
||||
function toObject(arr, f) {
|
||||
var keyValues = []
|
||||
var len = arr.length
|
||||
for(var i=0; i<len; i++) {
|
||||
keyValues.push(f(arr[i], i))
|
||||
}
|
||||
return merge.apply(arr, keyValues)
|
||||
}
|
||||
|
||||
/** Functional, nondestructive version of Array.prototype.splice.
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} index
|
||||
@param {Number} howMany
|
||||
@param {...*} elements
|
||||
*/
|
||||
function spliced(arr, index, howMany/*, elements*/) {
|
||||
var elements = Array.prototype.slice.apply(arguments, [3])
|
||||
var elementsLen = elements.length
|
||||
var results = []
|
||||
var len = arr.length
|
||||
|
||||
// add starting elements
|
||||
for(var i=0; i<index && i<len; i++) {
|
||||
results.push(arr[i])
|
||||
}
|
||||
|
||||
// add inserted elements
|
||||
for(i=0; i<elementsLen; i++) {
|
||||
results.push(elements[i])
|
||||
}
|
||||
|
||||
// add ending elements
|
||||
for(i=index+howMany; i<len; i++) {
|
||||
results.push(arr[i])
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
/** Breaks up the array into n evenly-sized chunks.
|
||||
Solution from http://stackoverflow.com/questions/8188548/splitting-a-js-array-into-n-arrays
|
||||
@memberOf module:cint#
|
||||
@param {Array} arr
|
||||
@param {Number} n
|
||||
*/
|
||||
function chunk(a, n) {
|
||||
var len = a.length,out = [], i = 0
|
||||
while (i < len) {
|
||||
var size = Math.ceil((len - i) / n--)
|
||||
out.push(a.slice(i, i += size))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Object
|
||||
***********************************/
|
||||
|
||||
/** Returns a new object with the given key and value.
|
||||
@memberOf module:cint#
|
||||
@param {String} key
|
||||
@param value
|
||||
*/
|
||||
function keyValue(key, value) {
|
||||
var o = {}
|
||||
o[key] = value
|
||||
return o
|
||||
}
|
||||
|
||||
/** Gets the value of a key of the given object.
|
||||
*/
|
||||
function getValue(o, key) {
|
||||
return o[key];
|
||||
}
|
||||
|
||||
/** Sets the value of the given key and returns the object.
|
||||
*/
|
||||
function setValue(o, key, value) {
|
||||
o[key] = value
|
||||
return o
|
||||
}
|
||||
|
||||
/** Creates a mapping function that applies the given function to the value of the specified key.
|
||||
*/
|
||||
function mapOverKey(f, originalKey, newKey) {
|
||||
return function(o) {
|
||||
return setValue(o, newKey || originalKey, f(o[originalKey]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Join the object into a single string with the given separators separating properties from each other as well as values.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {String} propSeparator
|
||||
@param {String} valueSeparator
|
||||
*/
|
||||
function joinObject(obj, propSeparator, valueSeparator) {
|
||||
var keyValuePairs = []
|
||||
for(var prop in obj) {
|
||||
keyValuePairs.push(prop + valueSeparator + obj[prop])
|
||||
}
|
||||
return keyValuePairs.join(propSeparator)
|
||||
}
|
||||
|
||||
/** Returns a new object with the given objects merged onto it. Non-undefined properties on later arguments override identical properties on earlier arguments.
|
||||
@memberOf module:cint#
|
||||
@param {...Object} objects
|
||||
*/
|
||||
function merge(/*obj1, obj2, obj3, ...*/) {
|
||||
|
||||
var mothership = {}
|
||||
|
||||
// iterate through each given object
|
||||
var len = arguments.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var outlier = arguments[i]
|
||||
|
||||
// add each property to the mothership
|
||||
for(var prop in outlier) {
|
||||
if(typeOf(outlier[prop]) === 'object' && outlier[prop].constructor === Object && outlier[prop] !== null && !(outlier[prop] instanceof Array)) {
|
||||
mothership[prop] = merge(mothership[prop], outlier[prop]) // RECURSION
|
||||
}
|
||||
else if(outlier[prop] !== undefined) {
|
||||
mothership[prop] = outlier[prop]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mothership
|
||||
}
|
||||
|
||||
/** Returns a new object where f(key, value) returns a new key-value pair for each property.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {Function} f
|
||||
*/
|
||||
function mapObject(obj, f) {
|
||||
var result = {}
|
||||
for(var key in obj) {
|
||||
var pair = f(key, obj[key])
|
||||
for(var prop in pair) {
|
||||
result[prop] = pair[prop]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns an array whose items are the result of calling f(key, value) on each property of the given object. If f is undefined, returns a list of { key: ___, value: ___ } objects.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {Function} f
|
||||
*/
|
||||
function toArray(obj, f) {
|
||||
f = f || function(key, value) { return { key: key, value: value } }
|
||||
var result = []
|
||||
for(var key in obj) {
|
||||
result.push(f(key, obj[key]))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a new object that only includes the properties of the given obj for which f(key, value) is true.
|
||||
@memberOf module:cint#
|
||||
@param {Object} obj
|
||||
@param {Function} f
|
||||
*/
|
||||
function filterObject(obj, f) {
|
||||
var result = {}
|
||||
for(var key in obj) {
|
||||
if(f(key, obj[key])) {
|
||||
result[key] = obj[key]
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Changes the specified keys in an object.
|
||||
@param {Object} obj
|
||||
@param {Object} changeKeys
|
||||
@example cint.changeKeys(
|
||||
{ fname: 'Raine', lname: 'Lourie', specialty: 'Javascript' },
|
||||
{ fname: 'first', lname: 'last' }
|
||||
)
|
||||
*/
|
||||
function changeKeys(obj, changedKeys) {
|
||||
var result = {}
|
||||
for(var key in obj) {
|
||||
result[key in changedKeys ? changedKeys[key] : key] = obj[key]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** Calls a function on an object and returns the object (for chaining purposes).
|
||||
*/
|
||||
function tap(f, o) {
|
||||
f(o)
|
||||
return o
|
||||
}
|
||||
|
||||
/* console.log's the given object and returns the object (for chaining purposes).
|
||||
*/
|
||||
function look(o) {
|
||||
console.log(o)
|
||||
return o
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
* Function
|
||||
***********************************/
|
||||
|
||||
/** Returns a function that returns the inverse of the given boolean function.
|
||||
@memberOf module:cint#
|
||||
@param {function} f The function to inverse.
|
||||
*/
|
||||
function not(f) {
|
||||
return function() {
|
||||
return !f.apply(this, arguments)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a new function that inserts the given curried arguments to the inner function at the specified index of its runtime arguments.
|
||||
@memberOf module:cint#
|
||||
@example _.partial(f, args...) is equivalent to cint.partialAt(f, 0, args) and _.partialRight(f, args...) is equivalent to cint.partialAt(f, n, args) where n is the arity of the function.
|
||||
@param {Function} f
|
||||
@param {Number} index
|
||||
@param {...*} curriedArgs
|
||||
*/
|
||||
function partialAt(f, index, curriedArgs) {
|
||||
return function() {
|
||||
var givenArgs = Array.prototype.slice.call(arguments)
|
||||
|
||||
// handle negative indices
|
||||
// Note that we add 1 so that -1 points to the slot AFTER the last element, not before the last element (which is what the last index points to).
|
||||
if(index < 0) {
|
||||
index = circ(givenArgs, index) + 1
|
||||
}
|
||||
|
||||
var spliceArgs = [givenArgs, index, 0].concat(curriedArgs)
|
||||
var newArgs = spliced.apply(this, spliceArgs)
|
||||
return f.apply(this, newArgs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a new function that calls the given function with a limit on the number of arguments.
|
||||
@memberOf module:cint#
|
||||
@param {Function} f
|
||||
@param {Number} n
|
||||
*/
|
||||
function aritize(f, n) {
|
||||
return function() {
|
||||
var givenArgs = Array.prototype.slice.call(arguments, 0, n)
|
||||
return f.apply(this, givenArgs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Recursively invokes the given function with no parameters until it returns a non-functional value.
|
||||
@memberOf module:cint#
|
||||
@param value
|
||||
*/
|
||||
function callTillValue(value) {
|
||||
return typeof value === 'function' ? callTillValue(value()) : value
|
||||
}
|
||||
|
||||
/** Returns a function that calls the given function as normal, then passes its inputs and output to the spier (defaults to console.log)
|
||||
@memberOf module:cint#
|
||||
@param {Function} f
|
||||
@param {Function} [spier=console.log]
|
||||
*/
|
||||
function spy(f, spier) {
|
||||
var that = this
|
||||
/* jshint ignore:start */
|
||||
spier = spier || console.log.bind(console)
|
||||
/* jshint ignore:end */
|
||||
|
||||
return function() {
|
||||
var args = Array.prototype.slice.call(arguments)
|
||||
var out = f.apply(that, args)
|
||||
spier.call(that, f, args, out)
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a copy of the given function that calls the original function in the context of the first argument. Passes arguments 1..n as normal.
|
||||
@memberOf module:cint#
|
||||
@param f
|
||||
*/
|
||||
function inContext(f) {
|
||||
return function(context) {
|
||||
var otherArgs = Array.prototype.slice.call(arguments, 1)
|
||||
return f.apply(context, otherArgs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Converts the given synchronous function into an asynchronous function that applies its arguments to the original function and invokes callback(error, result).
|
||||
@memberOf module:cint#
|
||||
@param f
|
||||
*/
|
||||
function toAsync(f) {
|
||||
return function(/* [arg1], [arg2], ..., callback */) {
|
||||
var that = this;
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var callback = _last(args);
|
||||
setTimeout(function() {
|
||||
try {
|
||||
var result = f.apply(that, args);
|
||||
callback(null, result);
|
||||
}
|
||||
catch(e) {
|
||||
callback(e);
|
||||
}
|
||||
}, 0)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***********************************
|
||||
* Utility
|
||||
***********************************/
|
||||
|
||||
/** Compares two items lexigraphically. Returns 1 if a>b, 0 if a==b, or -1 if a<b.
|
||||
@memberOf module:cint#
|
||||
@param a
|
||||
@param b
|
||||
*/
|
||||
function compare(a,b) {
|
||||
return a > b ? 1 :
|
||||
a < b ? -1 :
|
||||
0
|
||||
}
|
||||
|
||||
/** Compares the value of the given property between two objects.
|
||||
@memberOf module:cint#
|
||||
@param {String} property
|
||||
*/
|
||||
function compareProperty(property, a, b) {
|
||||
return compare(a[property], b[property])
|
||||
}
|
||||
|
||||
/** Returns a compare function that can be passed to Array.sort to sort in the order of the given array of properties. A property can also be appended with ' ASC' or ' DESC' to control the sort order.
|
||||
@memberOf module:cint#
|
||||
@param {String[]|Object[]} props
|
||||
*/
|
||||
function dynamicCompare(props) {
|
||||
|
||||
if(!props || !(props instanceof Array)) {
|
||||
throw new Error('props is falsey or not an Array')
|
||||
}
|
||||
|
||||
return function(a,b) {
|
||||
var len = props.length
|
||||
for(var i=0; i<len; i++) {
|
||||
var aVal, bVal, sortDir
|
||||
if(typeof props[i] == 'function') {
|
||||
aVal = props[i](a)
|
||||
bVal = props[i](b)
|
||||
sortDir = 'asc'
|
||||
}
|
||||
|
||||
else if(props[i].toLowerCase().indexOf(' ') >= 0) {
|
||||
var splitVal = props[i].split(' ')
|
||||
aVal = a[splitVal[0]]
|
||||
bVal = b[splitVal[0]]
|
||||
sortDir = splitVal[1].toLowerCase()
|
||||
}
|
||||
else {
|
||||
aVal = a[props[i]]
|
||||
bVal = b[props[i]]
|
||||
sortDir = 'asc'
|
||||
}
|
||||
|
||||
// this is important so that if the values are equial, it continues to the next sort property
|
||||
if(aVal != bVal) {
|
||||
return sortDir == 'asc' ? compare(aVal,bVal) : compare(bVal,aVal)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true if all the items in a are equal to all the items in b, recursively.
|
||||
@memberOf module:cint#
|
||||
@param a
|
||||
@param b
|
||||
*/
|
||||
function equals(a,b) {
|
||||
|
||||
if(typeof a !== typeof b) {
|
||||
return false
|
||||
}
|
||||
|
||||
// compare arrays
|
||||
if(a instanceof Array) {
|
||||
|
||||
// check if the arrays are the same length
|
||||
if(a.length !== b.length) {
|
||||
return false
|
||||
}
|
||||
|
||||
// check the equality of each item
|
||||
for(var i=0, l=a.length; i<l; i++) {
|
||||
if(!b || !b[i] || !equals(a[i], b[i])) { // RECURSION
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
// compare primitives
|
||||
else if(typeof a === 'number' || typeof a === 'string' || typeof a === 'boolean' || typeof a === 'undefined') {
|
||||
if(a !== b) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
// compare objects
|
||||
else if(Object.keys(a).length === Object.keys(b)) {
|
||||
for(var property in a) {
|
||||
if(!(property in b && b[property] === a[property])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return a === b
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/** Returns true if the given value is not undefined, null, or an empty string.
|
||||
@memberOf module:cint#
|
||||
@param x
|
||||
*/
|
||||
function isValue(x) {
|
||||
return x !== undefined && x !== null && x !== ''
|
||||
}
|
||||
|
||||
/** Returns a string representation of the given scalar, array, or dictionary.
|
||||
@memberOf module:cint#
|
||||
@param o
|
||||
*/
|
||||
function hash(o) {
|
||||
if(o === undefined) {
|
||||
return 'undefined'
|
||||
}
|
||||
else if(o === null) {
|
||||
return 'null'
|
||||
}
|
||||
else if(typeof o === 'string' || typeof o === 'number') {
|
||||
return '' + o
|
||||
}
|
||||
else if(typeOf(o) === 'array') {
|
||||
return '_[{0}]_'.format(o.map(hash).join(','))
|
||||
}
|
||||
else if(typeOf(o) === 'object') {
|
||||
var objString = ''
|
||||
for(var prop in o) {
|
||||
objString += supplant('{0}_:_{1}', [prop, hash(o[prop])])
|
||||
}
|
||||
// escape for handlebars
|
||||
return supplant('_\{{0}}_', [objString]) // jshint ignore:line
|
||||
}
|
||||
else {
|
||||
throw new Error('Unhashable value: ' + o)
|
||||
}
|
||||
}
|
||||
|
||||
/** Generates a pseudo-random string that can be assumed to be unique. */
|
||||
var guid = (function() {
|
||||
var S4 = function() {
|
||||
return (((1+Math.random())*0x10000)|0).toString(16).substring(1)
|
||||
}
|
||||
return function() {
|
||||
return S4()+S4()+'-'+S4()+'-'+S4()+'-'+S4()+'-'+S4()+S4()+S4()
|
||||
}
|
||||
})()
|
||||
|
||||
/** Returns a string representing the type of the object, with special handling for null and arrays.
|
||||
@author Douglas Crockford http://javascript.crockford.com/remedial.html
|
||||
@param value
|
||||
*/
|
||||
function typeOf(value) {
|
||||
var s = typeof value
|
||||
if (s === 'object') {
|
||||
if (value) {
|
||||
if (typeof value.length === 'number' &&
|
||||
!(value.propertyIsEnumerable('length')) &&
|
||||
typeof value.splice === 'function') {
|
||||
s = 'array'
|
||||
}
|
||||
} else {
|
||||
s = 'null'
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
/** Create a new instance of the given constructor with the given constructor arguments. Useful for higher order programmer where the new keyword won't work.
|
||||
@memberOf module:cint#
|
||||
@param {Function} C Constructor
|
||||
@param {Array} args
|
||||
*/
|
||||
function createNew(C, args) {
|
||||
var o = new C()
|
||||
C.apply(o, args)
|
||||
return o
|
||||
}
|
||||
|
||||
|
||||
/** Converts the given value to a string by calling its toString method.
|
||||
*/
|
||||
function intoString(value) {
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***********************************
|
||||
* Export Public Interface
|
||||
***********************************/
|
||||
|
||||
return {
|
||||
|
||||
// string
|
||||
supplant : supplant,
|
||||
startsWith : startsWith,
|
||||
before : before,
|
||||
after : after,
|
||||
between : between,
|
||||
bookend : bookend,
|
||||
repeatString : repeatString,
|
||||
toTitleCase : toTitleCase,
|
||||
|
||||
// number
|
||||
ordinal : ordinal,
|
||||
mapNumber : mapNumber,
|
||||
addTwo : addTwo,
|
||||
add : add,
|
||||
|
||||
// array
|
||||
orderedGroup : orderedGroup,
|
||||
tally : tally,
|
||||
tallyProps : tallyProps,
|
||||
index : index,
|
||||
rotate : rotate,
|
||||
toObject : toObject,
|
||||
spliced : spliced,
|
||||
chunk : chunk,
|
||||
|
||||
// object
|
||||
keyValue : keyValue,
|
||||
getValue : getValue,
|
||||
setValue : setValue,
|
||||
mapOverKey : mapOverKey,
|
||||
joinObject : joinObject,
|
||||
mapObject : mapObject,
|
||||
toArray : toArray,
|
||||
filterObject : filterObject,
|
||||
changeKeys : changeKeys,
|
||||
tap : tap,
|
||||
look : look,
|
||||
|
||||
// function
|
||||
not : not,
|
||||
partialAt : partialAt,
|
||||
aritize : aritize,
|
||||
callTillValue : callTillValue,
|
||||
spy : spy,
|
||||
inContext : inContext,
|
||||
toAsync : toAsync,
|
||||
|
||||
// utility
|
||||
compare : compare,
|
||||
compareProperty : compareProperty,
|
||||
dynamicCompare : dynamicCompare,
|
||||
equals : equals,
|
||||
isValue : isValue,
|
||||
hash : hash,
|
||||
guid : guid,
|
||||
typeOf : typeOf,
|
||||
'new' : createNew,
|
||||
intoString : intoString
|
||||
}
|
||||
|
||||
})()
|
||||
|
||||
// requirejs export
|
||||
if(typeof module !== 'undefined') {
|
||||
module.exports = cint
|
||||
}
|
701
node_modules/cint/test/spec.js
generated
vendored
Normal file
701
node_modules/cint/test/spec.js
generated
vendored
Normal file
|
@ -0,0 +1,701 @@
|
|||
var assert = require('chai').assert
|
||||
var cint = require('../src/cint.js')
|
||||
|
||||
/***********************************
|
||||
* String
|
||||
***********************************/
|
||||
|
||||
describe('supplant', function() {
|
||||
|
||||
it('should supplant an array by index', function() {
|
||||
assert.equal(cint.supplant('{0} walks his {1} in the {2}.',
|
||||
['Jim', 'dinosaur', 'park']),
|
||||
'Jim walks his dinosaur in the park.')
|
||||
})
|
||||
|
||||
it('should supplant an object by key', function() {
|
||||
assert.equal(cint.supplant('{owner} walks his {pet} in the {place}.',
|
||||
{ owner: 'Jim', pet: 'dinosaur', place: 'park' }),
|
||||
'Jim walks his dinosaur in the park.',
|
||||
'Supplant with object by key')
|
||||
})
|
||||
|
||||
it('should ignore non-existant keys', function() {
|
||||
assert.equal(cint.supplant('{owner} walks his {pet} in the {place}.',
|
||||
{ owner: 'Jim', pet: 'dinosaur' }),
|
||||
'Jim walks his dinosaur in the {place}.',
|
||||
'Ignores non-existant keys')
|
||||
})
|
||||
|
||||
it('should toString all values to be interpolated', function() {
|
||||
var Dino = function() {}
|
||||
Dino.prototype.toString = function() { return 'dinosaur' }
|
||||
assert.equal(cint.supplant('{owner} walks his {pet}.',
|
||||
{ owner: 'Jim', pet: new Dino() }),
|
||||
'Jim walks his dinosaur.'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('startsWith', function() {
|
||||
it('abc starts with a', function() {
|
||||
assert.equal(cint.startsWith('abc', 'a'), true)
|
||||
})
|
||||
it('abc doesn\'t start with c', function() {
|
||||
assert.equal(cint.startsWith('abc', 'c'), false)
|
||||
})
|
||||
it('Testing if something starts with empty string is always false.', function() {
|
||||
assert.equal(cint.startsWith('abc', ''), false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('before', function() {
|
||||
it('Return the substring before the given delimiter', function() {
|
||||
assert.equal(cint.before('ab,cd,ef', ','), 'ab')
|
||||
})
|
||||
})
|
||||
|
||||
describe('after', function() {
|
||||
it('Return the substring after the first given delimiter', function() {
|
||||
assert.equal(cint.after('ab,cd,ef', ','), 'cd,ef')
|
||||
})
|
||||
})
|
||||
|
||||
describe('between', function() {
|
||||
it('Return the substring between the given two delimiters', function() {
|
||||
assert.equal(cint.between('abcdef', 'ab', 'ef'), 'cd')
|
||||
})
|
||||
})
|
||||
|
||||
describe('bookend', function() {
|
||||
it('Add a string to the beginning and a string to the end of a string.', function() {
|
||||
assert.equal(cint.bookend('b', 'a', 'c'), 'abc')
|
||||
})
|
||||
it('Wrap a string with another string', function() {
|
||||
assert.equal(cint.bookend('b', 'a'), 'aba')
|
||||
})
|
||||
it('Ignores falsey begin and end values', function() {
|
||||
assert.equal(cint.bookend('b'), 'b')
|
||||
})
|
||||
})
|
||||
|
||||
describe('repeatString', function() {
|
||||
it('Repeat a string n times', function() {
|
||||
assert.equal(cint.repeatString('abc', 3), 'abcabcabc')
|
||||
})
|
||||
it('Repeating a string 0 times returns an empty string', function() {
|
||||
assert.equal(cint.repeatString('abc', 0), '')
|
||||
})
|
||||
})
|
||||
|
||||
describe('toTitleCase', function() {
|
||||
it('Capitalize the first letter of each word in a sentence', function() {
|
||||
assert.equal(cint.toTitleCase('this is a test'), 'This Is A Test')
|
||||
})
|
||||
it('Lowercase the rest', function() {
|
||||
assert.equal(cint.toTitleCase('tHis is A tEst'), 'This Is A Test')
|
||||
})
|
||||
it('Do not alter punctuation.', function() {
|
||||
assert.equal(cint.toTitleCase('don\'t fail the test.'), 'Don\'t Fail The Test.')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
/***********************************
|
||||
* Number
|
||||
***********************************/
|
||||
|
||||
describe('ordinal', function() {
|
||||
it('should add the correct ordinal', function() {
|
||||
assert.equal(cint.ordinal(1), '1st')
|
||||
assert.equal(cint.ordinal(2), '2nd')
|
||||
assert.equal(cint.ordinal(3), '3rd')
|
||||
assert.equal(cint.ordinal(4), '4th')
|
||||
assert.equal(cint.ordinal(5), '5th')
|
||||
assert.equal(cint.ordinal(6), '6th')
|
||||
assert.equal(cint.ordinal(7), '7th')
|
||||
assert.equal(cint.ordinal(8), '8th')
|
||||
assert.equal(cint.ordinal(9), '9th')
|
||||
assert.equal(cint.ordinal(10), '10th')
|
||||
assert.equal(cint.ordinal(11), '11th')
|
||||
assert.equal(cint.ordinal(12), '12th')
|
||||
assert.equal(cint.ordinal(13), '13th')
|
||||
assert.equal(cint.ordinal(14), '14th')
|
||||
assert.equal(cint.ordinal(15), '15th')
|
||||
assert.equal(cint.ordinal(16), '16th')
|
||||
assert.equal(cint.ordinal(17), '17th')
|
||||
assert.equal(cint.ordinal(18), '18th')
|
||||
assert.equal(cint.ordinal(19), '19th')
|
||||
assert.equal(cint.ordinal(20), '20th')
|
||||
assert.equal(cint.ordinal(21), '21st')
|
||||
assert.equal(cint.ordinal(22), '22nd')
|
||||
assert.equal(cint.ordinal(23), '23rd')
|
||||
assert.equal(cint.ordinal(24), '24th')
|
||||
assert.equal(cint.ordinal(100), '100th')
|
||||
assert.equal(cint.ordinal(101), '101st')
|
||||
assert.equal(cint.ordinal(102), '102nd')
|
||||
assert.equal(cint.ordinal(103), '103rd')
|
||||
assert.equal(cint.ordinal(104), '104th')
|
||||
})
|
||||
})
|
||||
|
||||
describe('mapNumber', function() {
|
||||
it('should map numbers', function() {
|
||||
var xTwo = function(n) { return n*2 }
|
||||
assert.deepEqual(cint.mapNumber(3, xTwo), [0, 2, 4])
|
||||
})
|
||||
})
|
||||
|
||||
describe('addTwo', function() {
|
||||
it('Add two numbers.', function() {
|
||||
assert.equal(cint.addTwo(4,5), 9)
|
||||
})
|
||||
})
|
||||
|
||||
describe('add', function() {
|
||||
it('No arguments returns 0', function() {
|
||||
assert.equal(cint.add(), 0)
|
||||
})
|
||||
it('1 argument returns the argument', function() {
|
||||
assert.equal(cint.add(1), 1)
|
||||
})
|
||||
it('2 arguments are added', function() {
|
||||
assert.equal(cint.add(1,2), 3)
|
||||
})
|
||||
it('More than 2 arguments are added', function() {
|
||||
assert.equal(cint.add(1,2,3), 6)
|
||||
})
|
||||
})
|
||||
|
||||
/***********************************
|
||||
* Array
|
||||
***********************************/
|
||||
|
||||
// test('orderedGroup', function() {
|
||||
// })
|
||||
|
||||
// test('tally', function() {
|
||||
// })
|
||||
|
||||
describe('tallyProps', function() {
|
||||
it('should tally property values', function() {
|
||||
|
||||
var data = [
|
||||
{
|
||||
ideal: 4,
|
||||
past: 3,
|
||||
present: 7
|
||||
},
|
||||
{
|
||||
ideal: 3,
|
||||
past: 7,
|
||||
present: 7
|
||||
}
|
||||
]
|
||||
|
||||
var expectedTallies = {
|
||||
"4": {
|
||||
ideal: 1
|
||||
},
|
||||
"3": {
|
||||
past: 1,
|
||||
ideal: 1
|
||||
},
|
||||
"7": {
|
||||
present: 2,
|
||||
past: 1
|
||||
}
|
||||
}
|
||||
|
||||
assert.deepEqual(cint.tallyProps(data), expectedTallies)
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
describe('index', function() {
|
||||
var arr = [1,2,3,4,5]
|
||||
it('Index into an array as normal', function() {
|
||||
assert.equal(cint.index(arr, 2), 3)
|
||||
})
|
||||
it('Negative index', function() {
|
||||
assert.equal(cint.index(arr, -1), 5)
|
||||
})
|
||||
it('Out of bounds index', function() {
|
||||
assert.equal(cint.index(arr, 16), 2)
|
||||
})
|
||||
|
||||
var str = 'abcde'
|
||||
it('Index into an array-like object as normal', function() {
|
||||
assert.equal(cint.index(str, 2), 'c')
|
||||
})
|
||||
it('Negative index of array-like object', function() {
|
||||
assert.equal(cint.index(str, -1), 'e')
|
||||
})
|
||||
it('Out of bounds index of array-like object', function() {
|
||||
assert.equal(cint.index(str, 16), 'b')
|
||||
})
|
||||
})
|
||||
|
||||
describe('rotate', function() {
|
||||
var arr = ['a', 'b', 'c']
|
||||
var resultArr = cint.rotate(arr, 0)
|
||||
it('Rotate array 0 spaces.', function() {
|
||||
assert.deepEqual(resultArr, ['a', 'b', 'c'])
|
||||
})
|
||||
it('Rotate array 1 space to the left.', function() {
|
||||
assert.deepEqual(cint.rotate(['a', 'b', 'c'], 1), ['b', 'c', 'a'])
|
||||
})
|
||||
it('Returns a new array instance', function() {
|
||||
assert.notEqual(arr, resultArr)
|
||||
})
|
||||
it('Does not modify the original array', function() {
|
||||
assert.deepEqual(arr, ['a', 'b', 'c'])
|
||||
})
|
||||
it('Rotate an array more spaces than its length.', function() {
|
||||
assert.deepEqual(cint.rotate(['a', 'b', 'c'], 5), ['c', 'a', 'b'])
|
||||
})
|
||||
it('Rotate array 1 space to the left.', function() {
|
||||
assert.deepEqual(cint.rotate(['a', 'b', 'c'], -1), ['c', 'a', 'b'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('toObject', function() {
|
||||
it('should convert an array to an object', function() {
|
||||
var animals = ['cat', 'dog', 'gecko']
|
||||
var pluralAnimals = cint.toObject(animals, function(animal) {
|
||||
return cint.keyValue(animal, animal + 's')
|
||||
})
|
||||
assert.deepEqual(pluralAnimals, {
|
||||
cat: 'cats',
|
||||
dog: 'dogs',
|
||||
gecko: 'geckos'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('spliced', function() {
|
||||
var arr = [1,2,3,4,5]
|
||||
it('should splice an array', function() {
|
||||
assert.deepEqual(cint.spliced(arr, 2, 1, 100, 200, 300), [1,2,100,200,300,4,5])
|
||||
})
|
||||
it('Original array is unchanged.', function() {
|
||||
assert.deepEqual(arr, [1,2,3,4,5])
|
||||
})
|
||||
})
|
||||
|
||||
describe('chunk', function() {
|
||||
var arr = [1,2,3,4,5,6,7,8,9,10]
|
||||
it('should split an array into chunks', function() {
|
||||
assert.deepEqual(cint.chunk(arr, 1), [[1,2,3,4,5,6,7,8,9,10]])
|
||||
assert.deepEqual(cint.chunk(arr, 2), [[1,2,3,4,5], [6,7,8,9,10]])
|
||||
assert.deepEqual(cint.chunk(arr, 3), [[1,2,3,4], [5,6,7], [8,9,10]])
|
||||
assert.deepEqual(cint.chunk(arr, 7), [[1,2], [3,4], [5,6], [7], [8], [9], [10]])
|
||||
assert.deepEqual(cint.chunk(arr, 10), [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
|
||||
})
|
||||
it('Original array is unchanged', function() {
|
||||
assert.deepEqual(arr, [1,2,3,4,5,6,7,8,9,10])
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
/***********************************
|
||||
* Object
|
||||
***********************************/
|
||||
|
||||
// object
|
||||
describe('keyValue', function() {
|
||||
it('Creates a key-value pair.', function() {
|
||||
assert.deepEqual(cint.keyValue('a',1), {a:1})
|
||||
})
|
||||
it('Creates a new object instance each time.', function() {
|
||||
assert.notEqual(cint.keyValue('a',1), cint.keyValue('a',1))
|
||||
})
|
||||
})
|
||||
|
||||
describe('getValue', function() {
|
||||
var o = {a:1}
|
||||
it('Gets the value of a key of the gven object', function() {
|
||||
assert.equal(cint.getValue(o, 'a'), 1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('setValue', function() {
|
||||
var o = {a:1}
|
||||
it('Sets the value of the given existing key', function() {
|
||||
cint.setValue(o, 'a', 2)
|
||||
assert.deepEqual(o, {a:2})
|
||||
})
|
||||
|
||||
it('Sets the value of the given new key', function() {
|
||||
cint.setValue(o, 'b', 10)
|
||||
assert.deepEqual(o, {a:2, b:10})
|
||||
})
|
||||
|
||||
it('Returns the object', function() {
|
||||
assert.equal(cint.setValue(o, 'a', 10), o)
|
||||
})
|
||||
})
|
||||
|
||||
describe('mapOverKey', function() {
|
||||
|
||||
var increment = function(n) { return ++n }
|
||||
var people = [
|
||||
{ name: 'Bob', age: 26 },
|
||||
{ name: 'Tia', age: 32 },
|
||||
{ name: 'José', age: 40 }
|
||||
]
|
||||
it('Maps the given function over the values of a key', function() {
|
||||
var olderPeople = [
|
||||
{ name: 'Bob', age: 27 },
|
||||
{ name: 'Tia', age: 33 },
|
||||
{ name: 'José', age: 41 }
|
||||
]
|
||||
var incrementAge = cint.mapOverKey(increment, 'age')
|
||||
assert.deepEqual(people.map(incrementAge), olderPeople)
|
||||
})
|
||||
|
||||
it('Maps the given function over the values of a key and assigns them to a new key', function() {
|
||||
var nextPeople = [
|
||||
{ name: 'Bob', age: 27, nextAge: 28 },
|
||||
{ name: 'Tia', age: 33, nextAge: 34 },
|
||||
{ name: 'José', age: 41, nextAge: 42 }
|
||||
]
|
||||
var incrementNextAge = cint.mapOverKey(increment, 'age', 'nextAge')
|
||||
assert.deepEqual(people.map(incrementNextAge), nextPeople)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('joinObject', function() {
|
||||
var o = { a: 1, b: 2, c: 3 }
|
||||
it('Join object keys and values together into a string', function() {
|
||||
assert.equal(cint.joinObject(o, '&', '='), 'a=1&b=2&c=3')
|
||||
})
|
||||
it('Empty object gets joined into an empty string', function() {
|
||||
assert.equal(cint.joinObject({}, '&', '='), '')
|
||||
})
|
||||
})
|
||||
|
||||
describe('mapObject', function() {
|
||||
it('should map an object to an object with new key/values', function() {
|
||||
var o = { a: 1, b: 2, c: 3 }
|
||||
var swap = function(k,v) { return cint.keyValue(v,k) }
|
||||
assert.deepEqual(cint.mapObject(o, swap), { '1': 'a', '2': 'b', '3': 'c' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('toArray', function() {
|
||||
var o = { a: 1, b: 2, c: 3 }
|
||||
it('should convert an object to an array', function() {
|
||||
assert.deepEqual(cint.toArray(o, function(key, value) {
|
||||
return key + '-' + value
|
||||
}), ['a-1', 'b-2', 'c-3'])
|
||||
})
|
||||
|
||||
it('should convert an object to an array of { key: ____, value: ____ } objects if no function is given.', function() {
|
||||
assert.deepEqual(cint.toArray(o), [
|
||||
{ key: 'a', value: 1 },
|
||||
{ key: 'b', value: 2 },
|
||||
{ key: 'c', value: 3 }
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('filterObject', function() {
|
||||
var o = { a: 1, b: 2, c: 3 }
|
||||
|
||||
it('Filter an object based on its keys and values', function() {
|
||||
assert.deepEqual(cint.filterObject(o, function(key, value) {
|
||||
return key !== 'b' && value !== 3
|
||||
}), { a: 1 })
|
||||
})
|
||||
|
||||
it('Original object is not modified.', function() {
|
||||
assert.deepEqual(o, { a: 1, b: 2, c: 3 })
|
||||
})
|
||||
})
|
||||
|
||||
describe('changeKeys', function() {
|
||||
// Assertions, ok, equal, notEqual, deepEqual, notDeepEqual, strictEqual, notStrictEqual
|
||||
var oldObject = { fname: 'Raine', lname: 'Lourie', specialty: 'Javascript' }
|
||||
var newObject = cint.changeKeys( oldObject, { fname: 'first', lname: 'last' })
|
||||
it('Old object is unmodified.', function() {
|
||||
assert.deepEqual(oldObject, { fname: 'Raine', lname: 'Lourie', specialty: 'Javascript' })
|
||||
})
|
||||
it('New object ontains modified keys.', function() {
|
||||
assert.deepEqual(newObject, { first: 'Raine', last: 'Lourie', specialty: 'Javascript' })
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('tap', function() {
|
||||
|
||||
var o = { a: 10 }
|
||||
var incrementA = function(o) { o.a++ }
|
||||
|
||||
it('Returns the given object', function() {
|
||||
assert.equal(cint.tap(incrementA, o), o)
|
||||
})
|
||||
it('Invokes the given function on the object', function() {
|
||||
assert.deepEqual(o, { a: 11 })
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('look', function() {
|
||||
it('Returns the argument', function() {
|
||||
assert.equal(cint.look('test:look'), 'test:look')
|
||||
})
|
||||
// Cant' really test the console.log
|
||||
})
|
||||
|
||||
|
||||
/***********************************
|
||||
* Function
|
||||
***********************************/
|
||||
|
||||
describe('not', function() {
|
||||
var yes = function() { return true }
|
||||
var no = function() { return false }
|
||||
var I = function(x) { return x }
|
||||
it('Reverses true to false.', function() {
|
||||
assert.equal(cint.not(yes)(), false)
|
||||
})
|
||||
it('Reverses false to true.', function() {
|
||||
assert.equal(cint.not(no)(), true)
|
||||
})
|
||||
it('Works with arguments.', function() {
|
||||
assert.equal(cint.not(I)(true), false)
|
||||
})
|
||||
it('Works with arguments.', function() {
|
||||
assert.equal(cint.not(I)(false), true)
|
||||
})
|
||||
it('Works with non-booleans.', function() {
|
||||
assert.equal(cint.not(I)('a'), false)
|
||||
})
|
||||
it('Works with non-booleans', function() {
|
||||
assert.equal(cint.not(I)(undefined), true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('partialAt', function() {
|
||||
var subtract = function(x,y) { return x - y }
|
||||
it('Inject arguments at the beginning.', function() {
|
||||
var subtractFromTen = cint.partialAt(subtract, 0, [10])
|
||||
assert.equal(subtractFromTen(1), 9)
|
||||
})
|
||||
|
||||
it('Inject arguments in the middle.', function() {
|
||||
var subtractTen = cint.partialAt(subtract, 1, [10])
|
||||
assert.equal(subtractTen(100), 90)
|
||||
})
|
||||
|
||||
it('Handles negative indexes', function() {
|
||||
var subtractTwenty = cint.partialAt(subtract, -1, [20])
|
||||
assert.equal(subtractTwenty(100), 80)
|
||||
})
|
||||
})
|
||||
|
||||
describe('aritize', function() {
|
||||
it('should limit the arity of a function', function() {
|
||||
var joinEm = function() {
|
||||
var givenArgs = Array.prototype.slice.call(arguments, 0)
|
||||
return givenArgs.join('')
|
||||
}
|
||||
var joinTwo = cint.aritize(joinEm, 2)
|
||||
assert.equal(joinTwo('a', 'b', 'c', 'd', 'e'), 'ab')
|
||||
})
|
||||
})
|
||||
|
||||
// describe('callTillValue', function() {
|
||||
// })
|
||||
|
||||
describe('spy', function() {
|
||||
it('should call a custom log function', function(done) {
|
||||
|
||||
function add(x, y) { return x + y }
|
||||
|
||||
function log(f, args, out) {
|
||||
assert.equal(f, add, 'first argument is the function')
|
||||
assert.deepEqual(args, [1,2], 'second argument is an array of arguments to that function')
|
||||
assert.equal(out, 3, 'third argument is the return value of the function')
|
||||
done()
|
||||
}
|
||||
|
||||
cint.spy(add, log)(1,2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('inContext', function() {
|
||||
var person = { name: 'Cecil' }
|
||||
it('calls the given function in the context of the first argument', function() {
|
||||
var getName = function() { return this.name }
|
||||
var getNameInContext = cint.inContext(getName)
|
||||
assert.equal(getNameInContext(person), 'Cecil')
|
||||
})
|
||||
|
||||
it('passes other arguments as normal', function() {
|
||||
var greet = function(greeting) { return greeting + ' ' + this.name }
|
||||
var greetInContext = cint.inContext(greet)
|
||||
assert.equal(greetInContext(person, 'Hi'), 'Hi Cecil')
|
||||
})
|
||||
})
|
||||
|
||||
describe('toAsync', function() {
|
||||
it('should return a function that invokes the given function and passes its results to a node-style callback', function(done) {
|
||||
function add(x, y) { return x + y }
|
||||
var addAsync = cint.toAsync(add)
|
||||
|
||||
addAsync(1, 2, function(error, result) {
|
||||
assert.notOk(error, 'error is null when successful')
|
||||
assert.equal(result, 3, 'gives the result in a callback')
|
||||
done()
|
||||
})
|
||||
})
|
||||
it('pass error as first argument to callback', function(done) {
|
||||
function toss() { throw new Error('Got tossed') }
|
||||
var tossAsync = cint.toAsync(toss)
|
||||
|
||||
tossAsync(function(error, result) {
|
||||
assert.instanceOf(error, Error)
|
||||
assert.equal(error.message, 'Got tossed')
|
||||
assert.notOk(result, 'no result')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
/***********************************
|
||||
* Utility
|
||||
***********************************/
|
||||
|
||||
describe('compare', function() {
|
||||
it('Returns 1 if a > b', function() {
|
||||
assert.equal(cint.compare(2,1), 1)
|
||||
})
|
||||
it('Returns 0 if a == b', function() {
|
||||
assert.equal(cint.compare(1,1), 0)
|
||||
})
|
||||
it('Returns -1 if a < b', function() {
|
||||
assert.equal(cint.compare(1,2), -1)
|
||||
})
|
||||
it('Returns 1 if a > b for strings', function() {
|
||||
assert.equal(cint.compare('ace', 'Ann'), 1)
|
||||
})
|
||||
it('Returns 0 if a == b for strings', function() {
|
||||
assert.equal(cint.compare('ace', 'ace'), 0)
|
||||
})
|
||||
it('Returns -1 if a < b for strings', function() {
|
||||
assert.equal(cint.compare('Ann', 'ace'), -1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('compareProperty', function() {
|
||||
var small = { n: 1 }
|
||||
var big = { n: 2 }
|
||||
var compareN = cint.compareProperty.bind(cint, 'n')
|
||||
it('Returns 1 if a > b', function() {
|
||||
assert.equal(compareN(big, small), 1)
|
||||
})
|
||||
it('Returns 0 if a == b', function() {
|
||||
assert.equal(compareN(big, big), 0)
|
||||
})
|
||||
it('Returns -1 if a < b', function() {
|
||||
assert.equal(compareN(small, big), -1)
|
||||
})
|
||||
})
|
||||
|
||||
// test('dynamicCompare', function() {
|
||||
// })
|
||||
|
||||
// test('equals', function() {
|
||||
// })
|
||||
|
||||
describe('isValue', function() {
|
||||
it('false is not valueful', function() {
|
||||
assert.equal(cint.isValue(null), false)
|
||||
})
|
||||
it('undefined is not valueful', function() {
|
||||
assert.equal(cint.isValue(undefined), false)
|
||||
})
|
||||
it('empty string is not valueful', function() {
|
||||
assert.equal(cint.isValue(''), false)
|
||||
})
|
||||
it('0 is valueful', function() {
|
||||
assert.equal(cint.isValue(0), true)
|
||||
})
|
||||
it('1 is valueful', function() {
|
||||
assert.equal(cint.isValue(1), true)
|
||||
})
|
||||
it('false is valueful', function() {
|
||||
assert.equal(cint.isValue(false), true)
|
||||
})
|
||||
it('true is valueful', function() {
|
||||
assert.equal(cint.isValue(true), true)
|
||||
})
|
||||
it('a string is valueful', function() {
|
||||
assert.equal(cint.isValue('test'), true)
|
||||
})
|
||||
})
|
||||
|
||||
// test('hash', function() {
|
||||
// })
|
||||
|
||||
// test('guid', function() {
|
||||
// })
|
||||
|
||||
describe('typeOf', function() {
|
||||
it('should work for null', function() {
|
||||
assert.equal(cint.typeOf(null), 'null')
|
||||
})
|
||||
it('should work for undefined', function() {
|
||||
assert.equal(cint.typeOf(undefined), 'undefined')
|
||||
})
|
||||
it('should work for string', function() {
|
||||
assert.equal(cint.typeOf('test'), 'string')
|
||||
})
|
||||
it('should work for number', function() {
|
||||
assert.equal(cint.typeOf(1), 'number')
|
||||
assert.equal(cint.typeOf(NaN), 'number')
|
||||
})
|
||||
it('should work for array', function() {
|
||||
assert.equal(cint.typeOf([]), 'array')
|
||||
})
|
||||
it('should work for object', function() {
|
||||
assert.equal(cint.typeOf({}), 'object')
|
||||
})
|
||||
it('should work for function', function() {
|
||||
assert.equal(cint.typeOf(function(){}), 'function')
|
||||
})
|
||||
})
|
||||
|
||||
describe('new', function() {
|
||||
it('should create an instance using the given constructor', function() {
|
||||
var Person = function(name, age) {
|
||||
this.name = name
|
||||
this.age = age
|
||||
}
|
||||
var p = cint.new(Person, ['Raine', 26])
|
||||
assert.instanceOf(p, Person)
|
||||
assert.equal(p.name, 'Raine')
|
||||
assert.equal(p.age, 26)
|
||||
})
|
||||
})
|
||||
|
||||
describe('intoString', function() {
|
||||
it('Converts a number to a string', function() {
|
||||
assert.equal(cint.intoString(4), '4')
|
||||
})
|
||||
it('Converts a boolean to a string', function() {
|
||||
assert.equal(cint.intoString(true), 'true')
|
||||
})
|
||||
it('should throw a TypeError if null is passed', function() {
|
||||
assert.throws(function() { cint.intoString(null) }, 'Cannot read property \'toString\' of null')
|
||||
})
|
||||
it('should throw a TypeError if undefined is passed', function() {
|
||||
assert.throws(
|
||||
cint.intoString.bind(cint, undefined), 'Cannot read property \'toString\' of undefined')
|
||||
})
|
||||
})
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue