Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pulling in string updates

  • Loading branch information...
commit 274b837cf8cbb1c0fe8903d304745d5f621faa85 1 parent e7ac520
mde authored
Showing with 667 additions and 158 deletions.
  1. +43 −12 lib/array.js
  2. +394 −100 lib/string.js
  3. +3 −2 test/file.js
  4. +222 −41 test/string.js
  5. +5 −3 test/xml.js
View
55 lib/array.js
@@ -1,5 +1,5 @@
/*
- * JSTools JavaScript utilities
+ * Geddy JavaScript Web development framework
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,25 +18,56 @@
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"
+ */
this.humanize = function(array) {
- // Return an array in a readable form, useful for outputting lists of items
+ // If array only has one item then just return it
+ if(array.length <= 1) {
+ return String(array);
+ }
- var last = array.pop();
- array = array.join(', ');
- return array + ' and ' + last;
+ var last = array.pop()
+ , items = array.join(', ');
+
+ return items + ' and ' + last;
};
+ /*
+ * 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
+ */
this.included = function(item, array) {
- // Check if an `item` is included in an `array`
- // If the `item` is found, it'll return and object with the key and value,
- // - otherwise return undefined
-
- if(!item) return undefined;
var result = array.indexOf(item);
if(result === -1) {
- return undefined;
- } else return { key: result, value: array[result] };
+ return false;
+ } else {
+ return array;
+ }
};
})();
View
494 lib/string.js
@@ -20,109 +20,273 @@ var core = require('./core')
, string;
string = new (function () {
- // Regexes used in trimming functions
- var _LTR = /^\s+/;
- var _RTR = /\s+$/;
- var _TR = /^\s+|\s+$/g;
- var _NL = /\n|\r|\r\n/g;
- // From/to char mappings -- for the XML escape,
- // unescape, and test for escapable chars
- var _CHARS = {
- '&': '&amp;',
- '<': '&lt;',
- '>': '&gt;',
- '"': '&quot;',
- '\'': '&#39;'
- };
- var _UUID_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
- // Builds the escape/unescape methods using a common
+
+ // Regexes for trimming, and character maps for escaping
+ var _LTR = /^\s+/
+ , _RTR = /\s+$/
+ , _TR = /^\s+|\s+$/g
+ , _NL = /\n|\r|\r\n/g
+ , _CHARS = {
+ '&': '&amp;'
+ , '<': '&lt;'
+ , '>': '&gt;'
+ , '"': '&quot;'
+ , '\'': '&#39;'
+ }
+ , _UUID_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
+ , _buildEscape
+ , _buildEscapeTest;
+
+ // Builds the escape/unescape methods using a
// map of characters
- var _buildEscapes = function (direction) {
- return function (str) {
- s = str || '';
- s = s.toString();
- var fr, to;
- for (var p in _CHARS) {
- fr = direction == 'to' ? p : _CHARS[p];
+ _buildEscape = function(direction) {
+ return function(string) {
+ string = string || '';
+ string = string.toString();
+
+ var from, to, p;
+ for(p in _CHARS) {
+ from = direction == 'to' ? p : _CHARS[p];
to = direction == 'to' ? _CHARS[p] : p;
- s = s.replace(new RegExp(fr, 'gm'), to);
+
+ string = string.replace(new RegExp(from, 'gm'), to);
}
- return s;
- };
+
+ return string;
+ }
};
- // Builds a method that tests for any of the escapable
- // characters -- useful for avoiding double-escaping if
- // you're not sure whether a string is already escaped
- var _buildEscapeTest = function (direction) {
- return function (s) {
- var pat = '';
- for (var p in _CHARS) {
+
+ // Builds a method that tests for any escapable
+ // characters, useful for avoiding double-scaping if
+ // you're not sure if a string has already been escaped
+ _buildEscapeTest = function(direction) {
+ return function(string) {
+ var pat = ''
+ , p;
+
+ for(p in _CHARS) {
pat += direction == 'to' ? p : _CHARS[p];
pat += '|';
}
+
pat = pat.substr(0, pat.length - 1);
pat = new RegExp(pat, "gm");
- return pat.test(s);
- };
+ return pat.test(string)
+ }
};
- // Escape special chars to entities
- this.escapeXML = _buildEscapes('to');
+ // Escape special XMl chars
+ this.escapeXML = _buildEscape('to');
- // Unescape entities to special chars
- this.unescapeXML = _buildEscapes('from');
+ // Unescape XML chars to literal representation
+ this.unescapeXML = _buildEscape('from');
- // Test if a string includes special chars that
- // require escaping
+ // Test if a string includes special chars
+ // that need escaping
this.needsEscape = _buildEscapeTest('to');
+ // Test if a string includes escaped chars
+ // that need unescaping
this.needsUnescape = _buildEscapeTest('from');
- this.toArray = function (str) {
- var arr = [];
- for (var i = 0; i < str.length; i++) {
- arr[i] = str.substr(i, 1);
+ /*
+ * toArray(string<String>)
+ *
+ * ToArray converts a String to an Array
+ *
+ * Examples:
+ * toArray("geddy")
+ * => ["g", "e", "d", "d", "y"]
+ */
+ this.toArray = function(string) {
+ if(!string) {
+ return;
+ }
+
+ var arr = []
+ , i = -1;
+
+ while(++i < string.length) {
+ arr.push(string.substr(i, 1));
}
+
return arr;
};
- this.reverse = function (str) {
- return this.toArray(str).reverse().join('');
+ /*
+ * reverse(string<String>)
+ *
+ * Reverse returns a Strings with `string` reversed
+ *
+ * Examples:
+ * reverse("yddeg")
+ * => "geddy"
+ */
+ this.reverse = function(string) {
+ if(!string) {
+ return;
+ }
+ return this.toArray(string).reverse().join('');
};
- this.ltrim = function (str, chr) {
- var pat = chr ? new RegExp('^' + chr + '+') : _LTR;
- return str.replace(pat, '');
+ /*
+ * 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
+ */
+ this.ltrim = function(string, char) {
+ if(!string) {
+ return;
+ }
+
+ var pat = char ? new RegExp('^' + char + '+') : _LTR;
+ return string.replace(pat, '');
};
- this.rtrim = function (str, chr) {
- var pat = chr ? new RegExp(chr + '+$') : _RTR;
- 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
+ */
+ this.rtrim = function (string, char) {
+ if(!string) {
+ return;
+ }
+
+ var pat = char ? new RegExp(char + '+$') : _RTR;
+ return string.replace(pat, '');
};
- this.trim = function (str, chr) {
- var pat = chr ? new RegExp('^' + chr + '+|' + chr + '+$', 'g') : _TR;
- 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
+ */
+ this.trim = function (string, char) {
+ if(!string) {
+ return;
+ }
+
+ var pat = char ? new RegExp('^' + char + '+|' + char + '+$', 'g') : _TR;
+ return string.replace(pat, '');
};
- this.lpad = function (str, chr, width) {
- var s = str || ''
- , len = s.length;
- if (width > len) {
- s = (new Array(width - len + 1)).join(chr) + s;
+ /*
+ * 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"
+ */
+ this.lpad = function(string, char, width) {
+ if(!string) {
+ return;
+ }
+
+ // Should width be string.length + 1? or the same to be safe
+ width = Number(width) || string.length;
+ char = char || ' ';
+ var s = string.toString();
+
+ while(s.length < width) {
+ s = char + s;
}
return s;
};
- this.rpad = function (str, chr, width) {
- var s = str || ''
- , len = s.length;
- if (width > len) {
- s = s + (new Array(width - len + 1)).join(chr);
+ /*
+ * 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&&"
+ */
+ this.rpad = function(string, char, width) {
+ if(!string) {
+ return;
+ }
+
+ // Should width be string.length + 1? or the same to be safe
+ width = Number(width) || string.length;
+ char = char || ' ';
+ var s = string;
+
+ while(s.length < width) {
+ s += char;
+ }
+ return s;
+ };
+
+ /*
+ * 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&"
+ */
+ this.pad = function(string, char, width) {
+ if(!string) {
+ return;
+ }
+
+ // Should width be string.length + 1? or the same to be safe
+ width = Number(width) || string.length;
+ char = char || ' ';
+ var s = string;
+
+ while(s.length < width) {
+ s = char + s + char;
}
return s;
};
+ /*
+ * 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...'
+ */
this.truncate = function(string, options, callback) {
if (!string) {
return;
@@ -204,20 +368,50 @@ string = new (function () {
}
};
+ /*
+ * 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>'
+ */
this.truncateHTML = function(string, options, callback) {
- if (!string) return;
- var returnString = '';
+ if(!string) {
+ return;
+ }
+ var returnString = ''
+ , opts = options;
// If `options` is a number assume it's the length and create a options object with length
- if (typeof options === 'number') {
- var num = options;
+ if(typeof opts === 'number') {
+ var num = opts;
- options = {};
- options.length = num;
- } else options = options || {};
+ opts = {};
+ opts.length = num;
+ } else opts = opts || {};
// Set `default` options for HTML specifics
- options.once = options.once || false;
+ opts.once = opts.once || false;
var pat = /(<[^>]*>)/ // Patter for matching HTML tags
, arr = [] // Holds the HTML tags and content seperately
@@ -225,14 +419,15 @@ string = new (function () {
, result = pat.exec(string)
, item
, firstPos
- , lastPos;
+ , lastPos
+ , i;
// Gather the HTML tags and content into the array
- while (result) {
+ while(result) {
firstPos = result.index;
lastPos = pat.lastIndex;
- if (firstPos !== 0) {
+ if(firstPos !== 0) {
// Should be content not HTML tags
arr.push(string.substring(0, firstPos));
// Slice content from string
@@ -245,11 +440,12 @@ string = new (function () {
// Re-run the pattern on the new string
result = pat.exec(string);
}
- if (string !== '') arr.push(string);
+ if(string !== '') arr.push(string);
// Loop through array items appending the tags to the string,
// - and truncating the text then appending it to content
- for(var i in arr) {
+ i = -1;
+ while(++i < arr.length) {
item = arr[i];
switch(true) {
// Closing tag
@@ -264,10 +460,10 @@ string = new (function () {
break;
// Normal text
default:
- if (options.once && truncated) {
+ if(opts.once && truncated) {
returnString += item;
} else {
- returnString += this.truncate(item, options, callback);
+ returnString += this.truncate(item, opts, callback);
truncated = true;
}
break;
@@ -277,12 +473,33 @@ string = new (function () {
return returnString;
};
+ /*
+ * nl2br(string<String>)
+ *
+ * Nl2br returns a string where all newline chars are turned
+ * into line break HTML tags
+ *
+ * Examples:
+ * nl2br("geddy\n")
+ * => "geddy<br />"
+ */
+ this.nl2br = function(string) {
+ if(!string) {
+ return;
+ }
- this.nl2br = function (str) {
- return str.replace(_NL,'<br />');
+ return string.replace(_NL,'<br />');
};
- // Converts someVariableName to some_variable_name
+ /*
+ * snakeize(string<String>)
+ *
+ * Snakeize converts camelCase and CamelCase strings to snake_case strings
+ *
+ * Examples:
+ * snakeize("geddyJs")
+ * => "geddy_js"
+ */
this.snakeize = (function () {
// Only create regexes once on initial load
var repl = /([A-Z]+)/g
@@ -302,7 +519,30 @@ string = new (function () {
this.decamelize = this.snakeize;
this.underscoreize = this.snakeize;
- // Converts some_variable_name to someVariableName or SomeVariableName
+ /*
+ * 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"
+ */
this.camelize = (function () {
// Only create regex once on initial load
var repl = /[-_](\w)/g;
@@ -342,22 +582,71 @@ string = new (function () {
};
}).call(this);
- this.capitalize = function (s) {
- return s.substr(0, 1).toUpperCase() + s.substr(1);
+ /*
+ * decapitalize(string<String>)
+ *
+ * Decapitalize returns the given string with the first letter
+ * uncapitalized.
+ *
+ * Examples:
+ * decapitalize("String")
+ * => "string"
+ */
+ this.decapitalize = function (string) {
+ if(!string) {
+ return;
+ }
+
+ return string.substr(0, 1).toLowerCase() + string.substr(1);
};
- this.decapitalize = function (s) {
- return s.substr(0, 1).toLowerCase() + s.substr(1);
+ /*
+ * capitalize(string<String>)
+ *
+ * Capitalize returns the given string with the first letter
+ * capitalized.
+ *
+ * Examples:
+ * decapitalize("string")
+ * => "String"
+ */
+ this.capitalize = function (string) {
+ if(!string) {
+ return;
+ }
+
+ return string.substr(0, 1).toUpperCase() + string.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.
+ */
this.dasherize = function(s, replace) {
- return this.snakeize(s, '-');
+ var repl = replace || '-'
+ return this.snakeize(s, repl);
+ };
+
+ /*
+ * underscorize(string<String>)
+ *
+ * Underscorize returns the given `string` converting camelCase and snakeCase
+ * to underscores.
+ */
+ this.underscorize = function(string) {
+ if(!string) {
+ return;
+ }
+
+ return this.dasherize(string, '_');
};
/*
* getInflections(name<String>, initialCap<String>)
*
- * Returns an object that contains different inflections
+ * Inflection returns an object that contains different inflections
* created from the given `name`
*/
this.getInflections = function (name, options) {
@@ -406,19 +695,23 @@ string = new (function () {
};
};
- // From Math.uuid.js, http://www.broofa.com/Tools/Math.uuid.js
+ // From Math.uuid.js, https://github.com/broofa/node-uuid
// Robert Kieffer (robert@broofa.com), MIT license
- this.uuid = function (len, rad) {
+ this.uuid = function(length, radix) {
var chars = _UUID_CHARS
, uuid = []
- , radix = rad || chars.length
- , r;
+ , r
+ , i;
- if (len) {
+ radix = radix || chars.length;
+
+ if(length) {
// Compact form
- for (var i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
- }
- else {
+ i = -1;
+ while(++i < length) {
+ uuid[i] = chars[0 | Math.random()*radix];
+ }
+ } else {
// rfc4122, version 4 form
// rfc4122 requires these characters
@@ -427,7 +720,8 @@ string = new (function () {
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
- for (var i = 0; i < 36; i++) {
+ i = -1;
+ while(++i < 36) {
if (!uuid[i]) {
r = 0 | Math.random()*16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
View
5 test/file.js
@@ -1,8 +1,9 @@
var assert = require('assert')
, fs = require('fs')
- , file = require('../lib/file');
+ , file = require('../lib/file')
+ , tests;
-var tests = {
+tests = {
'before': function () {
process.chdir('./test');
View
263 test/string.js
@@ -1,66 +1,247 @@
var assert = require('assert')
- , string = require('../lib/string');
+ , string = require('../lib/string')
+ , tests
+ , checkObjects;
+
+checkObjects = function (expected, actual) {
+ var item;
+ for (var p in actual) {
+ item = actual[p];
+ for (var q in item) {
+ assert.equal(expected[p][q], item[q]);
+ }
+ }
+};
tests = {
- 'test reverse': function () {
- var str = 'abcdef'
- , actual = string.reverse(str);
- assert.equal('fedcba', actual);
+ 'test toArray for string': function() {
+ var data = string.toArray('geddy')
+ , actual = ['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;
+ while(--i >= 0) {
+ assert.equal(data[i], actual[i]);
+ }
+ }
+
+, 'test reverse for string': function() {
+ var data = string.reverse('yddeg')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test basic ltrim for string': function() {
+ var data = string.ltrim(' geddy')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test custom char ltrim for string': function() {
+ var data = string.ltrim('&&geddy', '&')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test basic rtrim for string': function() {
+ var data = string.rtrim('geddy ')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test custom char rtrim for string': function() {
+ var data = string.rtrim('geddy&&', '&')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test basic trim for string': function() {
+ var data = string.trim(' geddy ')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test custom char trim for string': function() {
+ var data = string.trim('&geddy&&', '&')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test basic lpad for string': function() {
+ var data = string.lpad('geddy', '&', 7)
+ , actual = '&&geddy';
+ assert.equal(data, actual);
}
-, 'test ltrim': function () {
- var str = ' foo'
- , actual = string.ltrim(str);
- assert.equal('foo', actual);
+, 'test lpad without width for string': function() {
+ var data = string.lpad('geddy', '&')
+ , actual = 'geddy';
+ assert.equal(data, actual);
}
-, 'test rtrim': function () {
- var str = 'foo '
- , actual = string.rtrim(str);
- assert.equal('foo', actual);
+, 'test lpad without width of char for string': function() {
+ var data = string.lpad('geddy')
+ , actual = 'geddy';
+ assert.equal(data, actual);
}
-, 'test trim': function () {
- var str = ' foo '
- , actual = string.trim(str);
- assert.equal('foo', actual);
+, 'test basic rpad for string': function() {
+ var data = string.rpad('geddy', '&', 7)
+ , actual = 'geddy&&';
+ assert.equal(data, actual);
}
-, 'test lpad': function () {
- var str = 'foo'
- , actual = string.lpad(str, 'x', 7);
- assert.equal('xxxxfoo', actual);
+, 'test rpad without width for string': function() {
+ var data = string.rpad('geddy', '&')
+ , actual = 'geddy';
+ assert.equal(data, actual);
}
-, 'test rpad': function () {
- var str = 'foo'
- , actual = string.rpad(str, 'x', 7);
- assert.equal('fooxxxx', actual);
+, 'test rpad without width of char for string': function() {
+ var data = string.rpad('geddy')
+ , actual = 'geddy';
+ assert.equal(data, actual);
}
-, 'test camelize basic': function () {
- var str = 'foo_bar_baz'
- , actual = string.camelize(str);
- assert.equal('fooBarBaz', actual);
+, 'test basic pad for string': function() {
+ var data = string.pad('geddy', '&', 7)
+ , actual = '&geddy&';
+ assert.equal(data, actual);
}
-, 'test camelize no initial cap from capitalized snake': function () {
- var str = 'Foo_bar_baz'
- , actual = string.camelize(str);
- assert.equal('fooBarBaz', actual);
+, 'test pad without width for string': function() {
+ var data = string.pad('geddy', '&')
+ , actual = 'geddy';
+ assert.equal(data, actual);
}
-, 'test camelize initial cap': function () {
- var str = 'foo_bar_baz'
- , actual = string.camelize(str, {initialCap: true});
- assert.equal('FooBarBaz', actual);
+, 'test pad without width of char for string': function() {
+ var data = string.pad('geddy')
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+ // We could test truncate and truncateHTML here, but helper tests already
+ // include tests for them
+
+, 'test nl2br for string': function() {
+ var data = string.nl2br("geddy\n")
+ , actual = 'geddy<br />';
+ assert.equal(data, actual);
+ }
+
+, 'test snakeize for string': function() {
+ var data = string.snakeize("geddyJs")
+ , actual = 'geddy_js';
+ assert.equal(data, actual);
+ }
+
+, 'test snakeize with beginning caps for string': function() {
+ var data = string.snakeize("GeddyJs")
+ , actual = 'geddy_js';
+ assert.equal(data, actual);
+ }
+
+, 'test camelize for string': function() {
+ var data = string.camelize("geddy_js")
+ , actual = 'geddyJs';
+ assert.equal(data, actual);
+ }
+
+, 'test camelize with initialCap for string': function() {
+ var data = string.camelize("geddy_js", {initialCap: true})
+ , actual = 'GeddyJs';
+ assert.equal(data, actual);
+ }
+
+, 'test camelize with leadingUnderscore with no underscore for string': function() {
+ var data = string.camelize("geddy_js", {leadingUnderscore: true})
+ , actual = 'geddyJs';
+ assert.equal(data, actual);
+ }
+
+, 'test camelize with leadingUnderscore with underscore for string': function() {
+ var data = string.camelize("_geddy_js", {leadingUnderscore: true})
+ , actual = '_geddyJs';
+ assert.equal(data, actual);
+ }
+
+, 'test decapitalize for string': function() {
+ var data = string.decapitalize("Geddy")
+ , actual = 'geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test capitalize for string': function() {
+ var data = string.capitalize("geddy")
+ , actual = 'Geddy';
+ assert.equal(data, actual);
+ }
+
+, 'test dasherize for string': function() {
+ var data = string.dasherize("geddyJs")
+ , actual = 'geddy-js';
+ assert.equal(data, actual);
+ }
+
+, 'test dasherize with custom replace char for string': function() {
+ var data = string.dasherize("geddyJs", "_")
+ , actual = 'geddy_js';
+ assert.equal(data, actual);
+ }
+
+, 'test underscorize for string': function() {
+ var data = string.underscorize("geddyJs")
+ , actual = 'geddy_js';
+ assert.equal(data, actual);
+ }
+
+, 'test inflection for string': function() {
+ var actual = string.getInflections("string")
+ , expected = {
+ filename: {
+ singular: "string"
+ , plural: "strings"
+ },
+ constructor: {
+ singular: "String"
+ , plural: "Strings"
+ },
+ property: {
+ singular: "string"
+ , plural: "strings"
+ },
+ };
+
+ checkObjects(expected, actual);
+ }
+
+, 'test inflection with odd name for string': function() {
+ var actual = string.getInflections("snow_dog")
+ , expected = {
+ filename: {
+ singular: "snow_dog"
+ , plural: "snow_dogs"
+ },
+ constructor: {
+ singular: "SnowDog"
+ , plural: "SnowDogs"
+ },
+ property: {
+ singular: "snowDog"
+ , plural: "snowDogs"
+ },
+ };
+
+ checkObjects(expected, actual);
}
-, 'test camelize leading underscore': function () {
- var str = '_foo_bar_baz'
- , actual = string.camelize(str, {leadingUnderscore: true});
- assert.equal('_fooBarBaz', actual);
+, 'test uuid length for string': function() {
+ var data = string.uuid(5).length
+ , actual = 5;
+ assert.equal(data, actual);
}
};
View
8 test/xml.js
@@ -3,13 +3,15 @@ var XML = require('../lib/xml').XML
, assert = require('assert')
, obj
, xml
- , res;
+ , res
+ , serialize
+ , tests;
-var serialize = function (o) {
+serialize = function (o) {
return XML.stringify(o, {whitespace: false});
};
-var tests = {
+tests = {
'test serialized object': function () {
obj = {foo: 'bar'};
Please sign in to comment.
Something went wrong with that request. Please try again.