Comparing version 0.0.6 to 0.0.7
@@ -19,19 +19,16 @@ /* | ||
/** | ||
@name array | ||
@namespace array | ||
*/ | ||
var array = new (function () { | ||
/* | ||
* humanize(array<Array>) | ||
* | ||
* Humanize returns a string of the array items in a | ||
* readable format | ||
* | ||
* Examples: | ||
* humanize(["array", "array", "array"]) | ||
* => "array, array and array" | ||
* | ||
* humanize(["array", "array"]) | ||
* => "array and array" | ||
* | ||
* humanize(["array"]) | ||
* => "array" | ||
/** | ||
@name array#humanize | ||
@public | ||
@function | ||
@return {String} A string containing the array elements in a readable format | ||
@description Creates a string containing the array elements in a readable format | ||
@param {Array} array The array to humanize | ||
*/ | ||
@@ -50,15 +47,10 @@ this.humanize = function(array) { | ||
/* | ||
* included(item<Any>, array<Array>) | ||
* | ||
* Included checks if an `item` is included in a `array` | ||
* if it is found then the `array` is returned, otherwise | ||
* false is returned | ||
* | ||
* Examples: | ||
* included("array", ["array"]) | ||
* => ["array"] | ||
* | ||
* included("nope", ["array"]) | ||
* => false | ||
/** | ||
@name array#included | ||
@public | ||
@function | ||
@return {Array/Boolean} If `item` is included the `array` is returned otherwise false | ||
@description Checks if an `item` is included in an `array` | ||
@param {Any} item The item to look for | ||
@param {Array} array The array to check | ||
*/ | ||
@@ -75,4 +67,29 @@ this.included = function(item, array) { | ||
/** | ||
@name array#include | ||
@public | ||
@function | ||
@return {Boolean} Return true if the item is included in the array | ||
@description Checks if an `item` is included in an `array` | ||
@param {Array} array The array to check | ||
@param {Any} item The item to look for | ||
*/ | ||
this.include = function(array, item) { | ||
var res = -1; | ||
if (typeof array.indexOf == 'function') { | ||
res = array.indexOf(item); | ||
} | ||
else { | ||
for (var i = 0, ii = array.length; i < ii; i++) { | ||
if (array[i] == item) { | ||
res = i; | ||
break; | ||
} | ||
} | ||
} | ||
return res > -1; | ||
}; | ||
})(); | ||
module.exports = array; |
@@ -19,3 +19,3 @@ /* | ||
async = {}; | ||
var async = {}; | ||
@@ -22,0 +22,0 @@ /* |
218
lib/date.js
@@ -22,2 +22,7 @@ /* | ||
/** | ||
@name date | ||
@namespace date | ||
*/ | ||
date = new (function () { | ||
@@ -59,7 +64,15 @@ var _this = this | ||
'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | ||
this.meridian = { | ||
this.meridiem = { | ||
'AM': 'AM', | ||
'PM': 'PM' | ||
} | ||
// compat | ||
this.meridian = this.meridiem | ||
/** | ||
@name date#supportedFormats | ||
@public | ||
@object | ||
@description List of supported strftime formats | ||
*/ | ||
this.supportedFormats = { | ||
@@ -81,3 +94,3 @@ // abbreviated weekday name according to the current locale | ||
// day of the month as a decimal number (range 01 to 31) | ||
'd': function (dt) { return _this.leftPad(dt.getDate(), 2, '0'); }, | ||
'd': function (dt) { return util.string.lpad(dt.getDate(), '0', 2); }, | ||
// same as %m/%d/%y | ||
@@ -87,3 +100,3 @@ 'D': function (dt) { return _this.strftime(dt, '%m/%d/%y') }, | ||
// preceded by a space (range ' 1' to '31') | ||
'e': function (dt) { return _this.leftPad(dt.getDate(), 2, ' '); }, | ||
'e': function (dt) { return util.string.lpad(dt.getDate(), ' ', 2); }, | ||
// month as a decimal number, a single digit is | ||
@@ -103,21 +116,21 @@ // preceded by a space (range ' 1' to '12') | ||
// 00 to 23) | ||
'H': function (dt) { return _this.leftPad(dt.getHours(), 2, '0'); }, | ||
'H': function (dt) { return util.string.lpad(dt.getHours(), '0', 2); }, | ||
// hour as a decimal number using a 12-hour clock (range | ||
// 01 to 12) | ||
'I': function (dt) { return _this.leftPad( | ||
_this.hrMil2Std(dt.getHours()), 2, '0'); }, | ||
'I': function (dt) { return utils.string.lpad( | ||
_this.hrMil2Std(dt.getHours()), '0', 2); }, | ||
// day of the year as a decimal number (range 001 to 366) | ||
'j': function (dt) { return _this.leftPad( | ||
_this.calcDays(dt), 3, '0'); }, | ||
'j': function (dt) { return utils.string.lpad( | ||
_this.calcDays(dt), '0', 3); }, | ||
// Hour as a decimal number using a 24-hour clock (range | ||
// 0 to 23 (space-padded)) | ||
'k': function (dt) { return _this.leftPad(dt.getHours(), 2, ' '); }, | ||
'k': function (dt) { return utils.string.lpad(dt.getHours(), ' ', 2); }, | ||
// Hour as a decimal number using a 12-hour clock (range | ||
// 1 to 12 (space-padded)) | ||
'l': function (dt) { return _this.leftPad( | ||
_this.hrMil2Std(dt.getHours()), 2, ' '); }, | ||
'l': function (dt) { return utils.string.lpad( | ||
_this.hrMil2Std(dt.getHours()), ' ', 2); }, | ||
// month as a decimal number (range 01 to 12) | ||
'm': function (dt) { return _this.leftPad((dt.getMonth()+1), 2, '0'); }, | ||
'm': function (dt) { return utils.string.lpad((dt.getMonth()+1), '0', 2); }, | ||
// minute as a decimal number | ||
'M': function (dt) { return _this.leftPad(dt.getMinutes(), 2, '0'); }, | ||
'M': function (dt) { return utils.string.lpad(dt.getMinutes(), '0', 2); }, | ||
// Linebreak | ||
@@ -133,3 +146,3 @@ 'n': function () { return '\n'; }, | ||
// second as a decimal number | ||
'S': function (dt) { return _this.leftPad(dt.getSeconds(), 2, '0'); }, | ||
'S': function (dt) { return util.string.lpad(dt.getSeconds(), '0', 2); }, | ||
// Tab char | ||
@@ -168,3 +181,3 @@ 't': function () { return '\t'; }, | ||
// year as a decimal number including the century | ||
'Y': function (dt) { return _this.leftPad(dt.getFullYear(), 4, '0'); }, | ||
'Y': function (dt) { return utils.string.lpad(dt.getFullYear(), '0', 4); }, | ||
// time zone or name or abbreviation | ||
@@ -177,2 +190,9 @@ 'z': function () { return _this.strftimeNotImplemented('z'); }, | ||
/** | ||
@name date#getSupportedFormats | ||
@public | ||
@function | ||
@description return the list of formats in a string | ||
@return {String} The list of supported formats | ||
*/ | ||
this.getSupportedFormats = function () { | ||
@@ -187,2 +207,11 @@ var str = ''; | ||
/** | ||
@name date#strftime | ||
@public | ||
@function | ||
@return {String} The `dt` formated with the given `format` | ||
@description Formats the given date with the strftime format | ||
@param {Date} dt the date object to format | ||
@param {String} format the format to convert the date to | ||
*/ | ||
this.strftime = function (dt, format) { | ||
@@ -218,11 +247,10 @@ if (!dt) { return '' } | ||
this.leftPad = function (instr, len, spacer) { | ||
return string.lpad(instr, spacer, len); | ||
}; | ||
/** | ||
* Calculate the century to which a particular year belongs | ||
* @param year Integer year number | ||
* @return Integer century number | ||
*/ | ||
@name date#calcCentury | ||
@public | ||
@function | ||
@return {String} The century for the given date | ||
@description Find the century for the given `year` | ||
@param {String} year The year to find the century for | ||
*/ | ||
this.calcCentury = function (year) { | ||
@@ -246,6 +274,9 @@ if(!year) { | ||
/** | ||
* Calculate the day number in the year a particular date is on | ||
* @param dt JavaScript date object | ||
* @return Integer day number in the year for the given date | ||
*/ | ||
@name date#calcDays | ||
@public | ||
@function | ||
@return {Number} The number of days so far for the given date | ||
@description Calculate the day number in the year a particular date is on | ||
@param {Date} dt The date to use | ||
*/ | ||
this.calcDays = function(dt) { | ||
@@ -281,18 +312,24 @@ var first = new Date(dt.getFullYear(), 0, 1); | ||
/** | ||
* Return 'AM' or 'PM' based on hour in 24-hour format | ||
* @param h Integer for hour in 24-hour format | ||
* @return String of either 'AM' or 'PM' based on hour number | ||
*/ | ||
@name date#getMeridiem | ||
@public | ||
@function | ||
@return {String} Return 'AM' or 'PM' based on hour in 24-hour format | ||
@description Return 'AM' or 'PM' based on hour in 24-hour format | ||
@param {Number} h The hour to check | ||
*/ | ||
this.getMeridiem = function (h) { | ||
return h > 11 ? this.meridian.PM : | ||
this.meridian.AM; | ||
return h > 11 ? this.meridiem.PM : | ||
this.meridiem.AM; | ||
}; | ||
// Compat for mispelled version | ||
// Compat | ||
this.getMeridian = this.getMeridiem; | ||
/** | ||
* Convert a 24-hour formatted hour to 12-hour format | ||
* @param hour Integer hour number | ||
* @return String for hour in 12-hour format -- may be string length of one | ||
*/ | ||
@name date#hrMil2Std | ||
@public | ||
@function | ||
@return {String} Return a 12 hour version of the given time | ||
@description Convert a 24-hour formatted hour to 12-hour format | ||
@param {String} hour The hour to convert | ||
*/ | ||
this.hrMil2Std = function (hour) { | ||
@@ -306,7 +343,10 @@ var h = typeof hour == 'number' ? hour : parseInt(hour); | ||
/** | ||
* Convert a 12-hour formatted hour with meridian flag to 24-hour format | ||
* @param hour Integer hour number | ||
* @param pm Boolean flag, if PM hour then set to true | ||
* @return String for hour in 24-hour format | ||
*/ | ||
@name date#hrStd2Mil | ||
@public | ||
@function | ||
@return {String} Return a 24 hour version of the given time | ||
@description Convert a 12-hour formatted hour with meridian flag to 24-hour format | ||
@param {String} hour The hour to convert | ||
@param {Boolean} If hour is PM then this should be true | ||
*/ | ||
this.hrStd2Mil = function (hour, pm) { | ||
@@ -348,10 +388,13 @@ var h = typeof hour == 'number' ? hour : parseInt(hour); | ||
/** | ||
* Add to a Date in intervals of different size, from | ||
* milliseconds to years | ||
* @param dt -- Date (or timestamp Number), date to increment | ||
* @param interv -- String, a constant representing the interval, | ||
* e.g. YEAR, MONTH, DAY. See this.dateParts | ||
* @param incr -- Number, how much to add to the date | ||
* @return Integer day number for 1-7 base week | ||
*/ | ||
@name date#add | ||
@public | ||
@function | ||
@return {Date} Incremented date | ||
@description Add to a Date in intervals of different size, from | ||
milliseconds to years | ||
@param {Date} dt Date (or timestamp Number), date to increment | ||
@param {String} interv a constant representing the interval, | ||
e.g. YEAR, MONTH, DAY. See this.dateParts | ||
@param {Number} incr how much to add to the date | ||
*/ | ||
this.add = function (dt, interv, incr) { | ||
@@ -366,3 +409,3 @@ if (typeof dt == 'number') { dt = new Date(dt); } | ||
var sum = new Date(dt); | ||
switch(key) { | ||
switch (key) { | ||
case dateParts.YEAR: | ||
@@ -452,11 +495,14 @@ sum.setFullYear(dt.getFullYear()+incr); | ||
/** | ||
* Get the difference in a specific unit of time (e.g., number | ||
* of months, weeks, days, etc.) between two dates. | ||
* @param date1 -- Date (or timestamp Number) | ||
* @param date2 -- Date (or timestamp Number) | ||
* @param interv -- String, a constant representing the interval, | ||
* e.g. YEAR, MONTH, DAY. See this.dateParts | ||
* @return Integer, number of (interv) units apart that | ||
* the two dates are | ||
*/ | ||
@name date#diff | ||
@public | ||
@function | ||
@return {Number} number of (interv) units apart that | ||
the two dates are | ||
@description Get the difference in a specific unit of time (e.g., number | ||
of months, weeks, days, etc.) between two dates. | ||
@param {Date} date1 First date to check | ||
@param {Date} date2 Date to compate `date1` with | ||
@param {String} interv a constant representing the interval, | ||
e.g. YEAR, MONTH, DAY. See this.dateParts | ||
*/ | ||
this.diff = function (date1, date2, interv) { | ||
@@ -615,2 +661,11 @@ // date1 | ||
/** | ||
@name date#parse | ||
@public | ||
@function | ||
@return {Date} a JavaScript Date object | ||
@description Convert various sorts of strings to JavaScript | ||
Date objects | ||
@param {String} val The string to convert to a Date | ||
*/ | ||
this.parse = function (val) { | ||
@@ -664,3 +719,4 @@ var dt | ||
if (matches) { | ||
reordered = [matches[0], 0, 1, 0, matches[1], matches[2], matches[3], matches[4], null]; | ||
reordered = [matches[0], 0, 1, 0, matches[1], | ||
matches[2], matches[3], matches[4], null]; | ||
matches = reordered; | ||
@@ -722,2 +778,15 @@ } | ||
/** | ||
@name date#relativeTime | ||
@public | ||
@function | ||
@return {String} A string describing the amount of time ago | ||
the passed-in Date is | ||
@description Convert a Date to an English sentence representing | ||
how long ago the Date was | ||
@param {Date} dt The Date to to convert to a relative time string | ||
@param {Object} [opts] | ||
@param {Boolean} [opts.abbreviated=false] Use short strings | ||
(e.g., '<1m') for the relative-time string | ||
*/ | ||
this.relativeTime = function (dt, options) { | ||
@@ -775,2 +844,27 @@ var opts = options || {} | ||
/** | ||
@name date#toISO8601 | ||
@public | ||
@function | ||
@return {String} A string describing the amount of time ago | ||
@description Convert a Date to an ISO8601-formatted string | ||
@param {Date} dt The Date to to convert to an ISO8601 string | ||
*/ | ||
var _pad = function (n) { | ||
return n < 10 ? '0' + n : n; | ||
}; | ||
this.toISO8601 = function (dt) { | ||
var str = dt.getFullYear() + '-' | ||
+ _pad(dt.getMonth() + 1) + '-' | ||
+ _pad(dt.getDate()) + 'T' | ||
+ _pad(dt.getHours()) + ':' | ||
+ _pad(dt.getMinutes()) + ':' | ||
+ _pad(dt.getSeconds()) + '.' | ||
+ dt.getMilliseconds() + 'Z'; | ||
return str; | ||
}; | ||
// Alias | ||
this.toIso8601 = this.toISO8601; | ||
})(); | ||
@@ -777,0 +871,0 @@ |
@@ -25,2 +25,3 @@ /* | ||
, async = require('./async') | ||
, i18n = require('./i18n') | ||
, uri = require('./uri') | ||
@@ -43,2 +44,3 @@ , array = require('./array') | ||
utils.async = async; | ||
utils.i18n = i18n; | ||
utils.uri = uri; | ||
@@ -45,0 +47,0 @@ utils.array = array; |
@@ -19,14 +19,20 @@ /* | ||
/** | ||
@name object | ||
@namespace object | ||
*/ | ||
var object = new (function () { | ||
/* | ||
* merge(object, otherObject) | ||
* | ||
* Merge merges `otherObject` into `object` and takes care of deep | ||
* merging of objects | ||
/** | ||
@name object#merge | ||
@public | ||
@function | ||
@return {Object} Returns the merged object | ||
@description Merge merges `otherObject` into `object` and takes care of deep | ||
merging of objects | ||
@param {Object} object Object to merge into | ||
@param {Object} otherObject Object to read from | ||
*/ | ||
this.merge = function(object, otherObject) { | ||
// Merges two objects together, then returns the final object | ||
// - if no matching value is found it will create a new one otherwise it will overwrite the old | ||
// - one, also supports deep merging automatically | ||
object = object || {}; | ||
@@ -53,6 +59,10 @@ otherObject = otherObject || {}; | ||
/* | ||
* reverseMerge(object<Object>, defaultObject<Object>) | ||
* | ||
* ReverseMerge merges `object` into `defaultObject` | ||
/** | ||
@name object#reverseMerge | ||
@public | ||
@function | ||
@return {Object} Returns the merged object | ||
@description ReverseMerge merges `object` into `defaultObject` | ||
@param {Object} object Object to read from | ||
@param {Object} defaultObject Object to merge into | ||
*/ | ||
@@ -65,6 +75,9 @@ this.reverseMerge = function(object, defaultObject) { | ||
/* | ||
* isEmpty(object<Object>) | ||
* | ||
* IsEmpty returns true if it's empty otherwise false | ||
/** | ||
@name object#isEmpty | ||
@public | ||
@function | ||
@return {Boolean} Returns true if empty false otherwise | ||
@description isEmpty checks if an Object is empty | ||
@param {Object} object Object to check if empty | ||
*/ | ||
@@ -77,11 +90,9 @@ this.isEmpty = function(object) { | ||
/* | ||
* toArray(object<Object>) | ||
* | ||
* ToArray takes each key/value in the `object` and puts in array with the value | ||
* as an object with the original key and value | ||
* | ||
* Examples: | ||
* toArray({ex: "json"}) | ||
* => [{key: "ex", value: "json"}] | ||
/** | ||
@name object#toArray | ||
@public | ||
@function | ||
@return {Array} Returns an array of objects each including the original key and value | ||
@description Converts an object to an array of objects each including the original key/value | ||
@param {Object} object Object to convert | ||
*/ | ||
@@ -88,0 +99,0 @@ this.toArray = function(object) { |
@@ -19,5 +19,11 @@ /* | ||
var core = require('./core') | ||
, inflection = require('../deps/inflection') | ||
, inflection = require('./inflection') | ||
, string; | ||
/** | ||
@name string | ||
@namespace string | ||
*/ | ||
string = new (function () { | ||
@@ -93,21 +99,38 @@ | ||
/* | ||
* toArray(string<String>) | ||
* | ||
* ToArray converts a String to an Array | ||
* | ||
* Examples: | ||
* toArray("geddy") | ||
* => ["g", "e", "d", "d", "y"] | ||
/** | ||
@name string#escapeRegExpChars | ||
@public | ||
@function | ||
@return {String} A string of escaped characters | ||
@description Escapes regex control-characters in strings | ||
used to build regexes dynamically | ||
@param {String} string The string of chars to escape | ||
*/ | ||
this.escapeRegExpChars = (function() { | ||
var specials = [ '^', '/', '.', '*', '+', '?', '|', '(', ')', | ||
'[', ']', '{', '}', '\\' ]; | ||
sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g'); | ||
return function (string) { | ||
var str = string || ''; | ||
str = String(str); | ||
return str.replace(sRE, '\\$1'); | ||
}; | ||
}).call(this); | ||
/** | ||
@name string#toArray | ||
@public | ||
@function | ||
@return {Array} Returns an array of characters | ||
@description Converts a string to an array | ||
@param {String} string The string to convert | ||
*/ | ||
this.toArray = function(string) { | ||
if(!string) { | ||
return; | ||
} | ||
var arr = [] | ||
var str = string || '' | ||
, arr = [] | ||
, i = -1; | ||
str = String(str); | ||
while(++i < string.length) { | ||
arr.push(string.substr(i, 1)); | ||
while (++i < str.length) { | ||
arr.push(str.substr(i, 1)); | ||
} | ||
@@ -118,182 +141,198 @@ | ||
/* | ||
* reverse(string<String>) | ||
* | ||
* Reverse returns a Strings with `string` reversed | ||
* | ||
* Examples: | ||
* reverse("yddeg") | ||
* => "geddy" | ||
/** | ||
@name string#reverse | ||
@public | ||
@function | ||
@return {String} Returns the `string` reversed | ||
@description Reverses a string | ||
@param {String} string The string to reverse | ||
*/ | ||
this.reverse = function(string) { | ||
if(!string) { | ||
return; | ||
} | ||
return this.toArray(string).reverse().join(''); | ||
var str = string || ''; | ||
str = String(str); | ||
return this.toArray(str).reverse().join(''); | ||
}; | ||
/* | ||
* ltrim(string<String>, char<String>) | ||
* | ||
* Ltrim trims `char` from the left of a `string` and returns it | ||
* if no `char` is given it will trim spaces | ||
/** | ||
@name string#ltrim | ||
@public | ||
@function | ||
@return {String} Returns the trimmed string | ||
@description Ltrim trims `char` from the left of a `string` and returns it | ||
if no `char` is given it will trim spaces | ||
@param {String} string The string to trim | ||
@param {String} char The character to trim | ||
*/ | ||
this.ltrim = function(string, char) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || '' | ||
, pat = char ? new RegExp('^' + char + '+') : _LTR; | ||
str = String(str); | ||
var pat = char ? new RegExp('^' + char + '+') : _LTR; | ||
return string.replace(pat, ''); | ||
return str.replace(pat, ''); | ||
}; | ||
/* | ||
* rtrim(string<String>, char<String>) | ||
* | ||
* Rtrim trims `char` from the right of a `string` and returns it | ||
* if no `char` is given it will trim spaces | ||
/** | ||
@name string#rtrim | ||
@public | ||
@function | ||
@return {String} Returns the trimmed string | ||
@description Rtrim trims `char` from the right of a `string` and returns it | ||
if no `char` is given it will trim spaces | ||
@param {String} string The string to trim | ||
@param {String} char The character to trim | ||
*/ | ||
this.rtrim = function (string, char) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || '' | ||
, pat = char ? new RegExp(char + '+$') : _RTR; | ||
str = String(str); | ||
var pat = char ? new RegExp(char + '+$') : _RTR; | ||
return string.replace(pat, ''); | ||
return str.replace(pat, ''); | ||
}; | ||
/* | ||
* trim(string<String>, char<String>) | ||
* | ||
* Trim trims `char` from the right and left of a `string` and returns it | ||
* if no `char` is given it will trim spaces | ||
// Alias | ||
this.chomp = this.rtrim; | ||
/** | ||
@name string#trim | ||
@public | ||
@function | ||
@return {String} Returns the trimmed string | ||
@description Trim trims `char` from the left and right of a `string` and returns it | ||
if no `char` is given it will trim spaces | ||
@param {String} string The string to trim | ||
@param {String} char The character to trim | ||
*/ | ||
this.trim = function (string, char) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || '' | ||
, pat = char ? new RegExp('^' + char + '+|' + char + '+$', 'g') : _TR; | ||
str = String(str); | ||
var pat = char ? new RegExp('^' + char + '+|' + char + '+$', 'g') : _TR; | ||
return string.replace(pat, ''); | ||
return str.replace(pat, ''); | ||
}; | ||
/* | ||
* lpad(string<String>, char<String>, width<Number>) | ||
* | ||
* Lpad adds `char` to the left of `string` until the length | ||
* of `string` is more than `width` | ||
* | ||
* Examples: | ||
* lpad("geddy", "&", 7) | ||
* => "&&geddy" | ||
/** | ||
@name string#chop | ||
@public | ||
@function | ||
@description Returns a new String with the last character removed. If the | ||
string ends with \r\n, both characters are removed. Applying chop to an | ||
empty string returns an empty string. | ||
@param {String} string to return with the last character removed. | ||
*/ | ||
this.lpad = function(string, char, width) { | ||
if(!string) { | ||
return; | ||
this.chop = function (string) { | ||
var index | ||
, str = string || ''; | ||
str = String(str); | ||
if (str.length) { | ||
// Special-case for \r\n | ||
index = str.indexOf('\r\n'); | ||
if (index == str.length - 2) { | ||
return str.substring(0, index); | ||
} | ||
return str.substring(0, str.length - 1); | ||
} | ||
else { | ||
return ''; | ||
} | ||
}; | ||
/** | ||
@name string#lpad | ||
@public | ||
@function | ||
@return {String} Returns the padded string | ||
@description Lpad adds `char` to the left of `string` until the length | ||
of `string` is more than `width` | ||
@param {String} string The string to pad | ||
@param {String} char The character to pad with | ||
@param {Number} width the width to pad to | ||
*/ | ||
this.lpad = function(string, char, width) { | ||
var str = string || '' | ||
, width; | ||
str = String(str); | ||
// Should width be string.length + 1? or the same to be safe | ||
width = Number(width) || string.length; | ||
width = parseInt(width, 10) || str.length; | ||
char = char || ' '; | ||
var s = string.toString(); | ||
while(s.length < width) { | ||
s = char + s; | ||
while(str.length < width) { | ||
str = char + str; | ||
} | ||
return s; | ||
return str; | ||
}; | ||
/* | ||
* rpad(string<String>, char<String>, width<Number>) | ||
* | ||
* Rpad adds `char` to the right of `string` until the length | ||
* of `string` is more than `width` | ||
* | ||
* Examples: | ||
* rpad("geddy", "&", 7) | ||
* => "geddy&&" | ||
/** | ||
@name string#rpad | ||
@public | ||
@function | ||
@return {String} Returns the padded string | ||
@description Rpad adds `char` to the right of `string` until the length | ||
of `string` is more than `width` | ||
@param {String} string The string to pad | ||
@param {String} char The character to pad with | ||
@param {Number} width the width to pad to | ||
*/ | ||
this.rpad = function(string, char, width) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || '' | ||
, width; | ||
str = String(str); | ||
// Should width be string.length + 1? or the same to be safe | ||
width = Number(width) || string.length; | ||
width = parseInt(width, 10) || str.length; | ||
char = char || ' '; | ||
var s = string; | ||
while(s.length < width) { | ||
s += char; | ||
while(str.length < width) { | ||
str += char; | ||
} | ||
return s; | ||
return str; | ||
}; | ||
/* | ||
* pad(string<String>, char<String>, width<Number>) | ||
* | ||
* Pad adds `char` to the right and left of `string` until the length | ||
* of `string` is more than `width` | ||
* | ||
* Examples: | ||
* pad("geddy", "&", 7) | ||
* => "&geddy&" | ||
/** | ||
@name string#pad | ||
@public | ||
@function | ||
@return {String} Returns the padded string | ||
@description Pad adds `char` to the left and right of `string` until the length | ||
of `string` is more than `width` | ||
@param {String} string The string to pad | ||
@param {String} char The character to pad with | ||
@param {Number} width the width to pad to | ||
*/ | ||
this.pad = function(string, char, width) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || '' | ||
, width; | ||
str = String(str); | ||
// Should width be string.length + 1? or the same to be safe | ||
width = Number(width) || string.length; | ||
width = parseInt(width, 10) || str.length; | ||
char = char || ' '; | ||
var s = string; | ||
while(s.length < width) { | ||
s = char + s + char; | ||
while(str.length < width) { | ||
str = char + str + char; | ||
} | ||
return s; | ||
return str; | ||
}; | ||
/* | ||
* truncate(string<String>, options<Integer/Object>, callback[Function]) | ||
* | ||
* Truncates a given `string` after a specified `length` if `string` is longer than | ||
* `length`. The last characters will be replaced with an `omission` for a total length not | ||
* exceeding `length`. If `callback` is given it will fire if `string` is truncated. | ||
* | ||
* Options: | ||
* length <Integer> Length the output string will be(default: 30) | ||
* len <Integer> Alias for length | ||
* omission <String> Replace last letters with an omission(default: '...') | ||
* ellipsis <String> Alias for omission | ||
* seperator <String>/<RegExp> Break the truncated text at the nearest `seperator` | ||
* | ||
* Warnings: | ||
* Please be aware that truncating HTML tags or entities may result in malformed HTML returned | ||
* | ||
* Examples: | ||
* truncate('Once upon a time in a world', { length: 10 }) | ||
* => 'Once up...' | ||
* | ||
* truncate('Once upon a time in a world', { length: 20, omission: '...(continued)' }) | ||
* => 'Once u...(continued)' | ||
* | ||
* truncate('Once upon a time in a world', { length: 15, seperator: /\s/ }) | ||
* => 'Once upon a...' | ||
* Normal Output: => 'Once upon a ...' | ||
* | ||
* truncate('Once upon a time in a world', { length: 15, seperator: ' ' }) | ||
* => 'Once upon a...' | ||
* Normal Output: => 'Once upon a ...' | ||
* | ||
* truncate('<p>Once upon a time</p>', { length: 20 }) | ||
* => '<p>Once upon a ti...' | ||
/** | ||
@name string#truncate | ||
@public | ||
@function | ||
@return {String} Returns the truncated string | ||
@description Truncates a given `string` after a specified `length` if `string` is longer than | ||
`length`. The last characters will be replaced with an `omission` for a total length | ||
not exceeding `length`. If `callback` is given it will fire if `string` is truncated. | ||
@param {String} string The string to truncate | ||
@param {Integer/Object} options Options for truncation, If options is an Integer it will be length | ||
@param {Integer} [options.length=string.length] Length the output string will be | ||
@param {Integer} [options.len] Alias for `length` | ||
@param {String} [options.omission='...'] Replace last characters with an omission | ||
@param {String} [options.ellipsis='...'] Alias for `omission` | ||
@param {String/RegExp} [options.seperator] Break the truncated test at the nearest `seperator` | ||
@param {Function} callback Callback is called only if a truncation is done | ||
*/ | ||
this.truncate = function(string, options, callback) { | ||
if (!string) { | ||
return; | ||
} | ||
var stringLen = string.length | ||
var str = string || '' | ||
, stringLen | ||
, opts | ||
@@ -310,2 +349,5 @@ , stringLenWithOmission | ||
str = String(str); | ||
stringLen = str.length | ||
// If `options` is a number, assume it's the length and | ||
@@ -323,3 +365,3 @@ // create a options object with length | ||
// Set `opts` defaults | ||
opts.length = opts.length || 30; | ||
opts.length = opts.length || stringLen; | ||
opts.omission = opts.omission || opts.ellipsis || '...'; | ||
@@ -341,3 +383,3 @@ | ||
} | ||
stringToWorkWith = string.substring(0, stringLenWithOmission + 1) | ||
stringToWorkWith = str.substring(0, stringLenWithOmission + 1) | ||
lastIndexOf = -1 | ||
@@ -354,3 +396,3 @@ nextStop = 0 | ||
// Seperator is a String | ||
last = string.lastIndexOf(opts.seperator, stringLenWithOmission); | ||
last = str.lastIndexOf(opts.seperator, stringLenWithOmission); | ||
} | ||
@@ -369,6 +411,6 @@ | ||
if (stringLen < opts.length) { | ||
return string; | ||
return str; | ||
} | ||
else { | ||
returnString = string.substring(0, last) + opts.omission; | ||
returnString = str.substring(0, last) + opts.omission; | ||
returnString += callback ? callback() : ''; | ||
@@ -379,38 +421,28 @@ return returnString; | ||
/* | ||
* truncateHTML(string<String>, options<Object>, callback[Function]) | ||
* | ||
* Truncates a given `string` inside HTML tags after a specified `length` if string` is longer than | ||
* `length`. The last characters will be replaced with an `omission` for a total length not | ||
* exceeding `length`. If `callback` is given it will fire if `string` is truncated. If `once` is | ||
* true only the first string in the first HTML tags will be truncated leaving the others as they | ||
* were | ||
* | ||
* Options: | ||
* once <Boolean> If true it will only truncate the first text found in the first | ||
* set of HTML tags(default: false) | ||
* | ||
* Notes: | ||
* * All options available in the `truncate` helper are also available in `truncateHTML` | ||
* * HTML tags will not be truncated, so return value will always be safe for rendering | ||
* | ||
* Examples: | ||
* truncateHTML('<p>Once upon a time in a world</p>', { length: 10 }) | ||
* => '<p>Once up...</p>' | ||
* | ||
* truncateHTML('<p>Once upon a time <small>in a world</small></p>', { length: 10 }) | ||
* => '<p>Once up...<small>in a wo...</small></p>' | ||
* | ||
* truncateHTML('<p>Once upon a time <small>in a world</small></p>', { length: 10, once: true }) | ||
* => '<p>Once up...<small>in a world</small></p>' | ||
/** | ||
@name string#truncateHTML | ||
@public | ||
@function | ||
@return {String} Returns the HTML safe truncated string | ||
@description Truncates a given `string` inside HTML tags after a specified `length` if string` | ||
is longer than `length`. The last characters will be replaced with an `omission` | ||
for a total length not exceeding `length`. If `callback` is given it will fire if | ||
`string` is truncated. If `once` is true only the first string in the first HTML | ||
tags will be truncated leaving the others as they were | ||
@param {String} string The string to truncate | ||
@param {Integer/Object} options Options for truncation, If options is an Integer it will be length | ||
all Object options are the same as `truncate` | ||
@param {Boolean} [options.once=false] If true, it will only be truncated once, otherwise the | ||
truncation will loop through all text inside HTML tags | ||
@param {Function} callback Callback is called only if a truncation is done | ||
*/ | ||
this.truncateHTML = function(string, options, callback) { | ||
if(!string) { | ||
return; | ||
} | ||
var returnString = '' | ||
var str = string || '' | ||
, returnString = '' | ||
, opts = options; | ||
str = String(str); | ||
// If `options` is a number assume it's the length and create a options object with length | ||
if(typeof opts === 'number') { | ||
if (typeof opts === 'number') { | ||
var num = opts; | ||
@@ -428,3 +460,3 @@ | ||
, truncated = false | ||
, result = pat.exec(string) | ||
, result = pat.exec(str) | ||
, item | ||
@@ -440,16 +472,16 @@ , firstPos | ||
if(firstPos !== 0) { | ||
if (firstPos !== 0) { | ||
// Should be content not HTML tags | ||
arr.push(string.substring(0, firstPos)); | ||
arr.push(str.substring(0, firstPos)); | ||
// Slice content from string | ||
string = string.slice(firstPos); | ||
str = str.slice(firstPos); | ||
} | ||
arr.push(result[0]); // Push HTML tags | ||
string = string.slice(result[0].length); | ||
str = str.slice(result[0].length); | ||
// Re-run the pattern on the new string | ||
result = pat.exec(string); | ||
result = pat.exec(str); | ||
} | ||
if(string !== '') arr.push(string); | ||
if (str !== '') arr.push(str); | ||
@@ -474,3 +506,3 @@ // Loop through array items appending the tags to the string, | ||
default: | ||
if(opts.once && truncated) { | ||
if (opts.once && truncated) { | ||
returnString += item; | ||
@@ -488,28 +520,26 @@ } else { | ||
/* | ||
* nl2br(string<String>) | ||
* | ||
* Nl2br returns a string where all newline chars are turned | ||
* into line break HTML tags | ||
* | ||
* Examples: | ||
* nl2br("geddy\n") | ||
* => "geddy<br />" | ||
/** | ||
@name string#nl2br | ||
@public | ||
@function | ||
@return {String} The string with converted newlines chars to br tags | ||
@description Nl2br returns a string where all newline chars are turned | ||
into line break HTML tags | ||
@param {String} string The string to convert | ||
*/ | ||
this.nl2br = function(string) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || ''; | ||
str = String(str); | ||
return string.replace(_NL,'<br />'); | ||
return str.replace(_NL,'<br />'); | ||
}; | ||
/* | ||
* snakeize(string<String>) | ||
* | ||
* Snakeize converts camelCase and CamelCase strings to snake_case strings | ||
* | ||
* Examples: | ||
* snakeize("geddyJs") | ||
* => "geddy_js" | ||
/** | ||
@name string#snakeize | ||
@public | ||
@function | ||
@return {String} The string in a snake_case version | ||
@description Snakeize converts camelCase and CamelCase strings to snake_case strings | ||
@param {String} string The string to convert to snake_case | ||
@param {String} separ='_' The seperator to use | ||
*/ | ||
@@ -520,8 +550,7 @@ this.snakeize = (function () { | ||
, lead = /^_/g; | ||
return function (str, separ) { | ||
if (!str) { | ||
return; | ||
} | ||
var sep = separ || '_' | ||
return function (string, separ) { | ||
var str = string || '' | ||
, sep = separ || '_' | ||
, leading = separ ? new RegExp('^' + sep, 'g') : lead; | ||
str = String(str); | ||
return str.replace(repl, sep + '$1').toLowerCase(). | ||
@@ -533,28 +562,29 @@ replace(leading, ''); | ||
// Aliases | ||
/** | ||
@name string#underscorize | ||
@public | ||
@function | ||
@return {String} The string in a underscorized version | ||
@description Underscorize returns the given `string` converting camelCase and snakeCase to underscores | ||
@param {String} string The string to underscorize | ||
*/ | ||
this.underscorize = this.snakeize; | ||
this.underscoreize = this.snakeize; | ||
this.decamelize = this.snakeize; | ||
this.underscoreize = this.snakeize; | ||
/* | ||
* camelize(string<String>, options<Object>) | ||
* | ||
* Camelize takes a string and optional options and | ||
* returns a camelCase version of the given `string` | ||
* | ||
* Options: | ||
* initialCap <Boolean> If initialCap is true the returned | ||
* string will have a capitalized first letter | ||
* leadingUnderscore <Boolean> If leadingUnderscore os true then if | ||
* an underscore exists at the beggining | ||
* of the string, it will stay there. | ||
* Otherwise it'll be removed. | ||
* | ||
* Examples: | ||
* camelize("geddy_js") | ||
* => "geddyJs" | ||
* | ||
* camelize("geddy_js", {initialCap: true}) | ||
* => "GeddyJs" | ||
* | ||
* camelize("geddy_js", {leadingUnderscore: true}) | ||
* => "_geddyJs" | ||
/** | ||
@name string#camelize | ||
@public | ||
@function | ||
@return {String} The string in a camelCase version | ||
@description Camelize takes a string and optional options and | ||
returns a camelCase version of the given `string` | ||
@param {String} string The string to convert to camelCase | ||
@param {Object} options | ||
@param {Boolean} [options.initialCap] If initialCap is true the returned | ||
string will have a capitalized first letter | ||
@param {Boolean} [options.leadingUnderscore] If leadingUnderscore os true then if | ||
an underscore exists at the beggining | ||
of the string, it will stay there. | ||
Otherwise it'll be removed. | ||
*/ | ||
@@ -564,4 +594,5 @@ this.camelize = (function () { | ||
var repl = /[-_](\w)/g; | ||
return function (str, options) { | ||
var ret | ||
return function (string, options) { | ||
var str = string || '' | ||
, ret | ||
, config = { | ||
@@ -573,5 +604,3 @@ initialCap: false | ||
if (!str) { | ||
return; | ||
} | ||
str = String(str); | ||
@@ -602,61 +631,63 @@ // Backward-compat | ||
/* | ||
* decapitalize(string<String>) | ||
* | ||
* Decapitalize returns the given string with the first letter | ||
* uncapitalized. | ||
* | ||
* Examples: | ||
* decapitalize("String") | ||
* => "string" | ||
/** | ||
@name string#decapitalize | ||
@public | ||
@function | ||
@return {String} The string with the first letter decapitalized | ||
@description Decapitalize returns the given string with the first letter uncapitalized. | ||
@param {String} string The string to decapitalize | ||
*/ | ||
this.decapitalize = function (string) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || ''; | ||
str = String(str); | ||
return string.substr(0, 1).toLowerCase() + string.substr(1); | ||
return str.substr(0, 1).toLowerCase() + str.substr(1); | ||
}; | ||
/* | ||
* capitalize(string<String>) | ||
* | ||
* Capitalize returns the given string with the first letter | ||
* capitalized. | ||
* | ||
* Examples: | ||
* decapitalize("string") | ||
* => "String" | ||
/** | ||
@name string#capitalize | ||
@public | ||
@function | ||
@return {String} The string with the first letter capitalized | ||
@description capitalize returns the given string with the first letter capitalized. | ||
@param {String} string The string to capitalize | ||
*/ | ||
this.capitalize = function (string) { | ||
if(!string) { | ||
return; | ||
} | ||
var str = string || ''; | ||
str = String(str); | ||
return string.substr(0, 1).toUpperCase() + string.substr(1); | ||
return str.substr(0, 1).toUpperCase() + str.substr(1); | ||
}; | ||
/* | ||
* dasherize(string<String>, replace<String>) | ||
* | ||
* Dasherize returns the given `string` converting camelCase and snakeCase | ||
* to dashes or replace them with the `replace` character. | ||
/** | ||
@name string#dasherize | ||
@public | ||
@function | ||
@return {String} The string in a dashed version | ||
@description Dasherize returns the given `string` converting camelCase and snakeCase | ||
to dashes or replace them with the `replace` character. | ||
@param {String} string The string to dasherize | ||
@param {String} replace='-' The character to replace with | ||
*/ | ||
this.dasherize = function(s, replace) { | ||
this.dasherize = function(string, replace) { | ||
var repl = replace || '-' | ||
return this.snakeize(s, repl); | ||
return this.snakeize(string, repl); | ||
}; | ||
/* | ||
* underscorize(string<String>) | ||
* | ||
* Underscorize returns the given `string` converting camelCase and snakeCase | ||
* to underscores. | ||
/** | ||
@name string#include | ||
@public | ||
@function | ||
@return {Boolean} Returns true if the string is found in the string to search | ||
@description Searches for a particular string in another string | ||
@param {String} searchIn The string to search for the other string in | ||
@param {String} searchFor The string to search for | ||
*/ | ||
this.underscorize = function(string) { | ||
if(!string) { | ||
return; | ||
this.include = function (searchIn, searchFor) { | ||
var str = searchFor; | ||
if (!str && typeof string != 'string') { | ||
return false; | ||
} | ||
return this.dasherize(string, '_'); | ||
str = String(str); | ||
return (searchIn.indexOf(str) > -1); | ||
}; | ||
@@ -670,2 +701,14 @@ | ||
*/ | ||
/** | ||
@name string#getInflections | ||
@public | ||
@function | ||
@return {Object} A Object containing multiple different inflects for the given `name` | ||
@description Inflection returns an object that contains different inflections | ||
created from the given `name` | ||
@param {String} string The string to create inflections from | ||
@param {Object} options | ||
@param {Boolean} [options.initialCap] | ||
*/ | ||
this.getInflections = function (name, options) { | ||
@@ -727,3 +770,3 @@ var opts = options || {} | ||
if(length) { | ||
if (length) { | ||
// Compact form | ||
@@ -730,0 +773,0 @@ i = -1; |
@@ -19,4 +19,9 @@ /* | ||
var core = require('./core') | ||
, inflection = require('../deps/inflection') | ||
, inflection = require('./inflection') | ||
/** | ||
@name xml | ||
@namespace xml | ||
*/ | ||
exports.XML = new (function () { | ||
@@ -221,10 +226,9 @@ | ||
/* | ||
* setIndentLevel(level<Number>) | ||
* | ||
* SetIndentLevel changes the indent level for XML.stringify and returns it | ||
* | ||
* Example: | ||
* setIndentLevel(5) | ||
* => 5 | ||
/** | ||
@name xml#setIndentLevel | ||
@public | ||
@function | ||
@return {Number} Return the given `level` | ||
@description SetIndentLevel changes the indent level for XML.stringify and returns it | ||
@param {Number} level The indent level to use | ||
*/ | ||
@@ -239,13 +243,15 @@ this.setIndentLevel = function (level) { | ||
/* | ||
* stringify(obj<Any>, opts<Object>) | ||
* | ||
* Stringify returns an XML representation of the given `obj` | ||
* | ||
* Options: | ||
* whitespace <Boolean> Don't insert indents and newlines after xml entities(Default: true) | ||
* name <String> Use custom name as global namespace(Default: type of object) | ||
* fragment <Boolean> If true no header fragment is added to the top(Default: false) | ||
* level <Number> Remove this many levels from the output(Default: 0) | ||
* arrayRoot <Boolean> (Default: true) | ||
/** | ||
@name xml#stringify | ||
@public | ||
@function | ||
@return {String} Return the XML entities of the given `obj` | ||
@description Stringify returns an XML representation of the given `obj` | ||
@param {Object} obj The object containing the XML entities to use | ||
@param {Object} opts | ||
@param {Boolean} [opts.whitespace=true] Don't insert indents and newlines after xml entities | ||
@param {String} [opts.name=typeof obj] Use custom name as global namespace | ||
@param {Boolean} [opts.fragment=false] If true no header fragment is added to the top | ||
@param {Number} [opts.level=0] Remove this many levels from the output | ||
@param {Boolean} [opts.arrayRoot=true] | ||
*/ | ||
@@ -252,0 +258,0 @@ this.stringify = function (obj, opts) { |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)", | ||
@@ -13,0 +13,0 @@ "main": "./lib/index.js", |
@@ -26,37 +26,34 @@ /* | ||
'test basic humanize for array': function () { | ||
var data = array.humanize(["array", "array", "array"]) | ||
, actual = "array, array and array"; | ||
assert.equal(data, actual); | ||
var actual = array.humanize(["array", "array", "array"]) | ||
, expected = "array, array and array"; | ||
assert.equal(expected, actual); | ||
} | ||
, 'test humanize with two items for array': function() { | ||
var data = array.humanize(["array", "array"]) | ||
, actual = "array and array"; | ||
assert.equal(data, actual); | ||
var actual = array.humanize(["array", "array"]) | ||
, expected = "array and array"; | ||
assert.equal(expected, actual); | ||
} | ||
, 'test humanize with two items for array': function() { | ||
var data = array.humanize(["array"]) | ||
, actual = "array"; | ||
assert.equal(data, actual); | ||
var actual = array.humanize(["array"]) | ||
, expected = "array"; | ||
assert.equal(expected, actual); | ||
} | ||
, 'test basic included for array': function() { | ||
, 'test basic include for array': function() { | ||
var test = ["array"] | ||
, data = array.included("array", test) | ||
, actual = test; | ||
assert.equal(data, actual); | ||
, actual = array.include(test, "array"); | ||
assert.equal(true, actual); | ||
} | ||
, 'test false included for array': function() { | ||
var data = array.included("nope", ["array"]) | ||
, actual = false; | ||
assert.equal(data, actual); | ||
, 'test false include for array': function() { | ||
var actual = array.include(["array"], 'nope'); | ||
assert.equal(false, actual); | ||
} | ||
, 'test false boolean included for array': function() { | ||
, 'test false boolean include for array': function() { | ||
var test = ["array", false] | ||
, data = array.included(false, test) | ||
, actual = test; | ||
assert.equal(data, actual); | ||
, actual = array.include(test, false); | ||
assert.equal(true, actual); | ||
} | ||
@@ -63,0 +60,0 @@ |
@@ -28,15 +28,9 @@ /* | ||
, actual = _date.getDay(); | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
, 'test leftPad for date': function() { | ||
var data = date.leftPad("geddy", 7, " ") | ||
, actual = " geddy"; | ||
assert.equal(data, actual); | ||
} | ||
, 'test calcCentury using current year for date': function() { | ||
var data = date.calcCentury() | ||
, actual = '21'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -47,3 +41,3 @@ | ||
, actual = '20'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -54,3 +48,3 @@ | ||
, actual = '1'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -61,13 +55,7 @@ | ||
, actual = (_date.getHours() > 11) ? 'PM' : 'AM'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
, 'test leftPad for date': function() { | ||
var data = date.leftPad("geddy", 7, " ") | ||
, actual = " geddy"; | ||
assert.equal(data, actual); | ||
} | ||
}; | ||
module.exports = tests; |
@@ -29,3 +29,3 @@ /* | ||
, actual = object.merge({user: 'geddy'}, {key: 'key'}); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -36,3 +36,3 @@ | ||
, actual = object.merge({user: 'geddy', key: 'geddyKey'}, {key: 'key'}); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -43,3 +43,3 @@ | ||
, actual = object.reverseMerge({user: 'geddy'}, {key: 'key'}); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -50,3 +50,3 @@ | ||
, actual = object.reverseMerge({user: 'geddy', key: 'geddyKey'}, {key: 'key'}); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -57,3 +57,3 @@ | ||
, actual = object.isEmpty({user: 'geddy'}); | ||
assert.equal(expected, actual); | ||
assert.equal(actual, expected); | ||
} | ||
@@ -64,3 +64,3 @@ | ||
, actual = object.isEmpty({}); | ||
assert.equal(expected, actual); | ||
assert.equal(actual, expected); | ||
} | ||
@@ -67,0 +67,0 @@ |
/* | ||
* Geddy JavaScript Web development framework | ||
* Utilities: A classic collection of JavaScript utilities | ||
* Copyright 2112 Matthew Eernisse (mde@fleegix.org) | ||
@@ -4,0 +4,0 @@ * |
@@ -25,11 +25,17 @@ /* | ||
'test toArray for string': function() { | ||
'test escapeRegExpCharacters': function () { | ||
var expected = '\\^\\/\\.\\*\\+\\?\\|\\(\\)\\[\\]\\{\\}\\\\' | ||
actual = string.escapeRegExpChars('^/.*+?|()[]{}\\'); | ||
assert.equal(expected, actual); | ||
} | ||
, 'test toArray for string': function() { | ||
var data = string.toArray('geddy') | ||
, actual = ['g', 'e', 'd', 'd', 'y']; | ||
, expected = ['g', 'e', 'd', 'd', 'y']; | ||
// Loop through each item and check | ||
// if not, then the arrays aren't _really_ the same | ||
var i = actual.length; | ||
var i = expected.length; | ||
while(--i >= 0) { | ||
assert.equal(data[i], actual[i]); | ||
assert.equal(expected[i], data[i]); | ||
} | ||
@@ -40,4 +46,4 @@ } | ||
var data = string.reverse('yddeg') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -47,4 +53,4 @@ | ||
var data = string.ltrim(' geddy') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -54,4 +60,4 @@ | ||
var data = string.ltrim('&&geddy', '&') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -61,4 +67,4 @@ | ||
var data = string.rtrim('geddy ') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -68,4 +74,4 @@ | ||
var data = string.rtrim('geddy&&', '&') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -75,4 +81,4 @@ | ||
var data = string.trim(' geddy ') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -82,10 +88,40 @@ | ||
var data = string.trim('&geddy&&', '&') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
, 'test chop special-case line-ending': function() { | ||
var expected = 'geddy' | ||
, actual = string.chop('geddy\r\n'); | ||
assert.equal(expected, actual); | ||
} | ||
, 'test chop not actual special-case line-ending': function() { | ||
var expected = 'geddy\n' | ||
, actual = string.chop('geddy\n\r'); | ||
assert.equal(expected, actual); | ||
} | ||
, 'test chop normal line-ending': function() { | ||
var expected = 'geddy' | ||
, actual = string.chop('geddy\n'); | ||
assert.equal(expected, actual); | ||
} | ||
, 'test chop whatever character': function() { | ||
var expected = 'gedd' | ||
, actual = string.chop('geddy'); | ||
assert.equal(expected, actual); | ||
} | ||
, 'test chop empty string': function() { | ||
var expected = '' | ||
, actual = string.chop(''); | ||
assert.equal(expected, actual); | ||
} | ||
, 'test basic lpad for string': function() { | ||
var data = string.lpad('geddy', '&', 7) | ||
, actual = '&&geddy'; | ||
assert.equal(data, actual); | ||
, expected = '&&geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -95,4 +131,4 @@ | ||
var data = string.lpad('geddy', '&') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -102,4 +138,4 @@ | ||
var data = string.lpad('geddy') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -109,4 +145,4 @@ | ||
var data = string.rpad('geddy', '&', 7) | ||
, actual = 'geddy&&'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy&&'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -116,4 +152,4 @@ | ||
var data = string.rpad('geddy', '&') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -123,4 +159,4 @@ | ||
var data = string.rpad('geddy') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -130,4 +166,4 @@ | ||
var data = string.pad('geddy', '&', 7) | ||
, actual = '&geddy&'; | ||
assert.equal(data, actual); | ||
, expected = '&geddy&'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -137,4 +173,4 @@ | ||
var data = string.pad('geddy', '&') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -144,4 +180,4 @@ | ||
var data = string.pad('geddy') | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -154,4 +190,4 @@ | ||
var data = string.nl2br("geddy\n") | ||
, actual = 'geddy<br />'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy<br />'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -161,4 +197,4 @@ | ||
var data = string.snakeize("geddyJs") | ||
, actual = 'geddy_js'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy_js'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -168,4 +204,4 @@ | ||
var data = string.snakeize("GeddyJs") | ||
, actual = 'geddy_js'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy_js'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -175,4 +211,4 @@ | ||
var data = string.camelize("geddy_js") | ||
, actual = 'geddyJs'; | ||
assert.equal(data, actual); | ||
, expected = 'geddyJs'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -182,4 +218,4 @@ | ||
var data = string.camelize("geddy_js", {initialCap: true}) | ||
, actual = 'GeddyJs'; | ||
assert.equal(data, actual); | ||
, expected = 'GeddyJs'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -189,4 +225,4 @@ | ||
var data = string.camelize("geddy_js", {leadingUnderscore: true}) | ||
, actual = 'geddyJs'; | ||
assert.equal(data, actual); | ||
, expected = 'geddyJs'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -196,4 +232,4 @@ | ||
var data = string.camelize("_geddy_js", {leadingUnderscore: true}) | ||
, actual = '_geddyJs'; | ||
assert.equal(data, actual); | ||
, expected = '_geddyJs'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -203,4 +239,4 @@ | ||
var data = string.decapitalize("Geddy") | ||
, actual = 'geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -210,4 +246,4 @@ | ||
var data = string.capitalize("geddy") | ||
, actual = 'Geddy'; | ||
assert.equal(data, actual); | ||
, expected = 'Geddy'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -217,4 +253,4 @@ | ||
var data = string.dasherize("geddyJs") | ||
, actual = 'geddy-js'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy-js'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -224,4 +260,4 @@ | ||
var data = string.dasherize("geddyJs", "_") | ||
, actual = 'geddy_js'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy_js'; | ||
assert.equal(expected, data); | ||
} | ||
@@ -231,7 +267,15 @@ | ||
var data = string.underscorize("geddyJs") | ||
, actual = 'geddy_js'; | ||
assert.equal(data, actual); | ||
, expected = 'geddy_js'; | ||
assert.equal(expected, data); | ||
} | ||
, 'test inflection for string': function() { | ||
, 'test include for string with included string': function () { | ||
assert.ok(string.include('foobarbaz', 'foo')); | ||
} | ||
, 'test include for string with not included string': function () { | ||
assert.ok(!string.include('foobarbaz', 'qux')); | ||
} | ||
, 'test getInflections for string': function() { | ||
var actual = string.getInflections("string") | ||
@@ -278,4 +322,4 @@ , expected = { | ||
var data = string.uuid(5).length | ||
, actual = 5; | ||
assert.equal(data, actual); | ||
, expected = 5; | ||
assert.equal(expected, data); | ||
} | ||
@@ -282,0 +326,0 @@ |
@@ -28,3 +28,3 @@ /* | ||
, actual = 'json'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -35,3 +35,3 @@ | ||
, actual = 'username=user&token=token&secret=secret'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -42,3 +42,3 @@ | ||
, actual = 'username=user&auth=token&auth=secret'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -49,3 +49,3 @@ | ||
, actual = 'username=user&token='; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -56,3 +56,3 @@ | ||
, actual = 'username=user&token=0'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -63,3 +63,3 @@ | ||
, actual = 'username=user&token='; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -70,3 +70,3 @@ | ||
, actual = 'username=user&token='; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -77,3 +77,3 @@ | ||
, actual = 'username=user&auth_token=token'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -84,3 +84,3 @@ | ||
, actual = 'username=user&token=%26lt%3Btoken'; | ||
assert.equal(data, actual); | ||
assert.equal(actual, data); | ||
} | ||
@@ -91,3 +91,3 @@ | ||
, actual = uri.objectify('name=user'); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -98,3 +98,3 @@ | ||
, actual = uri.objectify('name=user&name=user2'); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -105,3 +105,3 @@ | ||
, actual = uri.objectify('name=user&name=user2', {consolidate: false}); | ||
assert.deepEqual(expected, actual); | ||
assert.deepEqual(actual, expected); | ||
} | ||
@@ -108,0 +108,0 @@ |
@@ -71,3 +71,3 @@ /* | ||
, actual = 5; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -78,3 +78,3 @@ | ||
, actual = '<?xml version="1.0" encoding="UTF-8"?>\n<object>\n <user>name</user>\n</object>\n'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -86,3 +86,3 @@ | ||
<string>user</string>\n</strings>'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -93,3 +93,3 @@ | ||
, actual = '<?xml version="1.0" encoding="UTF-8"?><object><user>name</user></object>'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -100,3 +100,3 @@ | ||
, actual = '<?xml version="1.0" encoding="UTF-8"?>\n<omg>\n<user>name</user>\n</omg>\n'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -107,3 +107,3 @@ | ||
, actual = '<object>\n<user>name</user>\n</object>\n'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -114,3 +114,3 @@ | ||
, actual = '<?xml version="1.0" encoding="UTF-8"?>\n <user>name</user>\n'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -122,3 +122,3 @@ | ||
<string>user</string>\n</strings>'; | ||
assert.equal(data, actual) | ||
assert.equal(actual, data) | ||
} | ||
@@ -125,0 +125,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
152864
29
4676