Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DT] Added to parse the same optional config argument that format uses. #587

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
40f897e
Added to parse the same optional config argument that
Satyam Apr 5, 2013
f52d102
Moved the regular expression out of the method.
Satyam Apr 5, 2013
a76da4e
Merge branch 'dev-3.x' into numberparser
Satyam Apr 19, 2013
8fd4ddc
Added a parse function builder.
Satyam Apr 19, 2013
fa08886
I have to pay attention to JSHint messages.
Satyam Apr 19, 2013
138ce7d
Added some more tests for edge conditions.
Satyam Apr 20, 2013
4221ed0
Merge branch 'dev-3.x' into numberparser
Satyam Apr 20, 2013
cc37782
Merge branch 'dev-3.x' into numberparser
Satyam May 2, 2013
6db021a
Add Y.ArraySort.naturalCompare().
rgrove May 16, 2013
8ead382
Fix comment typo.
rgrove May 16, 2013
d84970a
Fix lint errors in ArraySort tests.
rgrove May 16, 2013
1a98a3d
Fix missing types in naturalCompare() API docs.
rgrove May 16, 2013
b2c4827
'foo10' would be sorted before 'foo2', not 'foo1'.
rgrove May 16, 2013
d729c68
Merge branch 'dev-3.x' into natural-sort
rgrove May 20, 2013
ae0d432
Build arraysort.
rgrove May 20, 2013
0c80188
Add missing unit tests.
rgrove May 20, 2013
bdf3fad
ScrollInfo: Use getBoundingClientRect() instead of DOM.getXY() for no…
rgrove May 21, 2013
e74830e
Build node-scroll-info.
rgrove May 21, 2013
69b90f5
Merge branch 'dev-3.x' into numberparser
Satyam May 22, 2013
9d3cac2
Added Histoy.md entry
Satyam May 22, 2013
42beec3
Merged new tests into existing suite.
Satyam May 22, 2013
5b527eb
Added tests for inputs that return null.
Satyam May 22, 2013
c24d2c6
Added a few more tests
Satyam May 23, 2013
9e431fa
A blank, non-empty string now returns null.
Satyam May 24, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/arraysort/arraysort-coverage.js

Large diffs are not rendered by default.

128 changes: 122 additions & 6 deletions build/arraysort/arraysort-debug.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
YUI.add('arraysort', function (Y, NAME) {

/*jshint expr:true, onevar:false */

/**
Provides a case-insenstive comparator which can be used for array sorting.
Provides comparator functions useful for sorting arrays.

@module arraysort
*/
**/

var LANG = Y.Lang,
ISVALUE = LANG.isValue,
ISSTRING = LANG.isString;

/**
Provides a case-insenstive comparator which can be used for array sorting.
Provides comparator functions useful for sorting arrays.

@class ArraySort
*/
@static
**/

Y.ArraySort = {
var ArraySort = Y.ArraySort = {
// -- Public Methods -------------------------------------------------------

/**
Comparator function for simple case-insensitive sorting of an array of
Expand All @@ -28,6 +32,7 @@ Y.ArraySort = {
@param desc {Boolean} `true` if sort direction is descending, `false` if
sort direction is ascending.
@return {Boolean} -1 when a < b. 0 when a == b. 1 when a > b.
@static
*/
compare: function(a, b, desc) {
if(!ISVALUE(a)) {
Expand Down Expand Up @@ -57,8 +62,119 @@ Y.ArraySort = {
else {
return 0;
}
}
},

/**
Performs a natural-order comparison of two strings or numbers (or a string
and a number). This ensures that a value like 'foo2' will be sorted before
'foo10', whereas a standard ASCII sort would sort 'foo10' first.

@example

var items = ['item10', 'item2', 'item1', 10, '1', 2];

items.sort(Y.ArraySort.naturalCompare);
console.log(items); // => ['1', 2, 10, 'item1', 'item2', 'item10']

@method naturalCompare
@param {Number|String} a First value to compare.
@param {Number|String} b Second value to compare.
@param {Object} [options] Options.
@param {Boolean} [options.caseSensitive=false] If `true`, a
case-sensitive comparison will be performed. By default the
comparison is case-insensitive.
@param {Boolean} [options.descending=false] If `true`, the sort order
will be reversed so that larger values are sorted before smaller
values.
@return {Number} `0` if the two items are equal, a negative number if _a_
should be sorted before _b_, or a positive number if _b_ should be
sorted before _a_.
@static
@since @SINCE@
**/
naturalCompare: function (a, b, options) {
// Coerce `a` and `b` to strings.
a += '';
b += '';

// Convert `a` and `b` to lowercase unless `options.caseSensitive` is
// truthy.
if (!options || !options.caseSensitive) {
a = a.toLowerCase();
b = b.toLowerCase();
}

// Split `a` and `b` into alpha parts and numeric parts.
var aParts = ArraySort._splitAlphaNum(a),
bParts = ArraySort._splitAlphaNum(b),
length = Math.min(aParts.length, bParts.length),
result = 0,

aPart,
bPart,
i;

// Compare each part of `a` with each part of `b`.
for (i = 0; i < length; i++) {
aPart = aParts[i];
bPart = bParts[i];

// If the two parts aren't equal, compare them and stop iterating.
if (aPart !== bPart) {
// First, try comparing them as numbers.
result = aPart - bPart;

// If that didn't work, compare them as strings. This falsiness
// check works because `result` can't be 0 (we checked for
// equality above) and NaN is falsy.
if (!result) {
result = aPart > bPart ? 1 : -1;
}

// At this point we know enough to be able to sort the two
// strings, so we don't need to compare any more parts.
break;
}
}

// If we get here and `result` is still 0, then sort the shorter string
// before the longer string.
result || (result = a.length - b.length);

// Return the result, flipping the order if `options.descending` is
// truthy.
return options && options.descending ? -result : result;
},

// -- Protected Methods ----------------------------------------------------

/**
Splits a string into an array of alpha character and digit character parts.

@example

Y.ArraySort._splitAlphaNum('abc123def456');
// => ['abc', '123', 'def', '456']

@method _splitAlphaNum
@param {String} string String to split.
@return {String[]} Array of alpha parts and digit parts.
@protected
@static
@since @SINCE@
**/
_splitAlphaNum: function (string) {
/*jshint boss:true */
var parts = [],
regex = /(\d+|\D+)/g,
match;

while (match = regex.exec(string)) { // assignment
parts.push(match[1]);
}

return parts;
}
};


Expand Down
2 changes: 1 addition & 1 deletion build/arraysort/arraysort-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 122 additions & 6 deletions build/arraysort/arraysort.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
YUI.add('arraysort', function (Y, NAME) {

/*jshint expr:true, onevar:false */

/**
Provides a case-insenstive comparator which can be used for array sorting.
Provides comparator functions useful for sorting arrays.

@module arraysort
*/
**/

var LANG = Y.Lang,
ISVALUE = LANG.isValue,
ISSTRING = LANG.isString;

/**
Provides a case-insenstive comparator which can be used for array sorting.
Provides comparator functions useful for sorting arrays.

@class ArraySort
*/
@static
**/

Y.ArraySort = {
var ArraySort = Y.ArraySort = {
// -- Public Methods -------------------------------------------------------

/**
Comparator function for simple case-insensitive sorting of an array of
Expand All @@ -28,6 +32,7 @@ Y.ArraySort = {
@param desc {Boolean} `true` if sort direction is descending, `false` if
sort direction is ascending.
@return {Boolean} -1 when a < b. 0 when a == b. 1 when a > b.
@static
*/
compare: function(a, b, desc) {
if(!ISVALUE(a)) {
Expand Down Expand Up @@ -57,8 +62,119 @@ Y.ArraySort = {
else {
return 0;
}
}
},

/**
Performs a natural-order comparison of two strings or numbers (or a string
and a number). This ensures that a value like 'foo2' will be sorted before
'foo10', whereas a standard ASCII sort would sort 'foo10' first.

@example

var items = ['item10', 'item2', 'item1', 10, '1', 2];

items.sort(Y.ArraySort.naturalCompare);
console.log(items); // => ['1', 2, 10, 'item1', 'item2', 'item10']

@method naturalCompare
@param {Number|String} a First value to compare.
@param {Number|String} b Second value to compare.
@param {Object} [options] Options.
@param {Boolean} [options.caseSensitive=false] If `true`, a
case-sensitive comparison will be performed. By default the
comparison is case-insensitive.
@param {Boolean} [options.descending=false] If `true`, the sort order
will be reversed so that larger values are sorted before smaller
values.
@return {Number} `0` if the two items are equal, a negative number if _a_
should be sorted before _b_, or a positive number if _b_ should be
sorted before _a_.
@static
@since @SINCE@
**/
naturalCompare: function (a, b, options) {
// Coerce `a` and `b` to strings.
a += '';
b += '';

// Convert `a` and `b` to lowercase unless `options.caseSensitive` is
// truthy.
if (!options || !options.caseSensitive) {
a = a.toLowerCase();
b = b.toLowerCase();
}

// Split `a` and `b` into alpha parts and numeric parts.
var aParts = ArraySort._splitAlphaNum(a),
bParts = ArraySort._splitAlphaNum(b),
length = Math.min(aParts.length, bParts.length),
result = 0,

aPart,
bPart,
i;

// Compare each part of `a` with each part of `b`.
for (i = 0; i < length; i++) {
aPart = aParts[i];
bPart = bParts[i];

// If the two parts aren't equal, compare them and stop iterating.
if (aPart !== bPart) {
// First, try comparing them as numbers.
result = aPart - bPart;

// If that didn't work, compare them as strings. This falsiness
// check works because `result` can't be 0 (we checked for
// equality above) and NaN is falsy.
if (!result) {
result = aPart > bPart ? 1 : -1;
}

// At this point we know enough to be able to sort the two
// strings, so we don't need to compare any more parts.
break;
}
}

// If we get here and `result` is still 0, then sort the shorter string
// before the longer string.
result || (result = a.length - b.length);

// Return the result, flipping the order if `options.descending` is
// truthy.
return options && options.descending ? -result : result;
},

// -- Protected Methods ----------------------------------------------------

/**
Splits a string into an array of alpha character and digit character parts.

@example

Y.ArraySort._splitAlphaNum('abc123def456');
// => ['abc', '123', 'def', '456']

@method _splitAlphaNum
@param {String} string String to split.
@return {String[]} Array of alpha parts and digit parts.
@protected
@static
@since @SINCE@
**/
_splitAlphaNum: function (string) {
/*jshint boss:true */
var parts = [],
regex = /(\d+|\D+)/g,
match;

while (match = regex.exec(string)) { // assignment
parts.push(match[1]);
}

return parts;
}
};


Expand Down
4 changes: 2 additions & 2 deletions build/node-scroll-info/node-scroll-info-coverage.js

Large diffs are not rendered by default.

Loading