Skip to content
Browse files

add test of natSort

  • Loading branch information...
1 parent bb2a76c commit 716cb362f13f256d9bdbb9d53cae38aa1bc2107f @minodisk committed
Showing with 448 additions and 443 deletions.
  1. +43 −4 README.md
  2. +16 −21 lib/browser/sorter.js
  3. +1 −1 lib/browser/sorter.min.js
  4. +16 −21 lib/node/sorter.js
  5. +14 −5 package.json
  6. +12 −27 src/sorter.coffee
  7. +346 −0 test/test-sorter.coffee
  8. +0 −364 test/test.coffee
View
47 README.md
@@ -1,13 +1,12 @@
# Sorter
Dictionary and natural sort module for Node.js, RequireJS and browser.
-Supports 2 bytes character in English and Japanese.
## Installation at Node.js
$ npm install sorter
-## Adding to browser
+## Adding to browser source
### RequireJS
@@ -29,5 +28,45 @@ Supports 2 bytes character in English and Japanese.
## API Documentation
-* **dictSort(array, key = null)** -
-* **natSort(array, key = null)** -
+* **dictSort(array\[, key\])** - Returns natural ordered array.
+* **natSort(array\[, key\])** - Returns dictionary ordered array.
+
+## Dictionary Sort
+
+Array#sort()
+
+ AD
+ AM
+ BC
+ PM
+ after
+ before
+
+sorter.dictSort()
+
+ AD
+ after
+ AM
+ BC
+ before
+ PM
+
+## Natural Sort
+
+Array#sort()
+
+ image_005.jpg
+ image_04.jpg
+ image_1.jpg
+ image_12.jpg
+ image_21.jpg
+ image_4.jpg
+
+sorter.natSort()
+
+ image_1.jpg
+ image_04.jpg
+ image_4.jpg
+ image_005.jpg
+ image_12.jpg
+ image_21.jpg
View
37 lib/browser/sorter.js
@@ -1,5 +1,5 @@
(function() {
- var DICTIONARY_DATA, R_MULTIBYTE_NUM, R_NUM_SPLITTER, dictCompare, dictionary, exports, natCompare, natParse, toDictCode;
+ var DICTIONARY_DATA, R_MULTIBYTE_NUM, R_NUM_SPLITTER, dictCompare, dictionary, exports, natCompare, toDictCode;
exports = {};
@@ -149,16 +149,28 @@
};
exports.natSort = function(src, key) {
- var dst, i, tmps, v, _len;
+ var chunks, dst, i, tmps, v, _len;
if (key == null) key = null;
tmps = [];
- console.log(src);
for (i = 0, _len = src.length; i < _len; i++) {
v = src[i];
v = key ? v[key] : v;
+ chunks = [];
+ v.replace(R_NUM_SPLITTER, function(matched, num, str) {
+ var chunk;
+ chunk = {};
+ if (num) {
+ num = num.replace(R_MULTIBYTE_NUM, function(matched) {
+ return '0123456789'.indexOf(matched);
+ });
+ chunk.num = Number(num);
+ }
+ chunk.str = matched;
+ return chunks.push(chunk);
+ });
tmps[i] = {
raw: v,
- chunks: natParse(v)
+ chunks: chunks
};
}
tmps.sort(natCompare);
@@ -170,23 +182,6 @@
return dst;
};
- natParse = function(text) {
- var chunks;
- chunks = [];
- text.replace(R_NUM_SPLITTER, function(matched, num, str) {
- var chunk;
- chunk = {};
- if (num) {
- chunk.num = Number(num.replace(R_MULTIBYTE_NUM, function(matched) {
- return '0123456789'.indexOf(matched);
- }));
- }
- chunk.str = matched;
- return chunks.push(chunk);
- });
- return chunks;
- };
-
natCompare = function(a, b) {
var d, delta, i, _ref, _ref2;
a = a.chunks;
View
2 lib/browser/sorter.min.js
@@ -1 +1 @@
-undefined
+(function(){var a,b,c,d,e,f,g,h;f={},c=/([0-9 \uff10-\uff19\u3000]+)|([^0-9 \uff10-\uff19\u3000]+)/g,b=/[\uff10-\uff19]/g,a={en:{encode:{ABCDEFGHIJKLMNOPQRSTUVWXYZ:["ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz","abcdefghijklmnopqrstuvwxyz"],"01234567890":["0123456789"]}},ru:{encode:{"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ":["абвгдеёжзийклмнопрстуфхцчшщъыьэюя"]}},ja:{encode:{"あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん":["ぁぃぅぇぉ っ ゃ ゅ ょ ゎ","ァィゥェォヵ ヶ ッ ャ ュ ョ ヮ","ァィゥェォ ッ ャ ュ ョ","あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん","アイウエオカキクケコサシスセソタチツデトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワヰ ヱヲン","アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワ ヲン"," ゔ がぎぐげござじずぜぞだぢづでど ばびぶべぼ"," ヴ ガギグゲゴザジズゼゾダヂヅデド バビブベボ"," ぱぴぷぺぽ"," パピプペポ"]},"long":{"ー":{"ぁぃぅぇぉ":[" っ","ゃ ゅ ょ","ゎ"],"ァィゥェォ":["ヵ ヶ"," ッ","ャ ュ ョ","ヮ"],"ァィゥェォ":[" ッ","ャ ュ ョ"],"あいうえお":["かきくけこ","さしすせそ","たちつてと","なにぬねの","はひふへほ","まみむめも","や ゆ よ","らりるれろ","わゐ ゑを"," ゔ","がぎぐげご","ざじずぜぞ","だぢづでど","ばびぶべぼ","ぱぴぷぺぽ"],"アイウエオ":["カキクケコ","サシスセソ","タチツデト","ナニヌネノ","ハヒフヘホ","マミムメモ","ヤ ユ ヨ","ラリルレロ","ワヰ ヱヲ"," ヴ","ガギグゲゴ","ザジズゼゾ","ダヂヅデド","バビブベボ","パピプペポ"],"アイウエオ":["カキクケコ","サシスセソ","タチツテト","ナニヌネノ","ハヒフヘホ","マミムメモ","ヤ ユ ヨ","ラリルレロ","ワ ヲ"]}}}},e={encode:[],"long":{}},function(){var b,c,d,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x;x=[];for(j in a){g=a[j],w=g.encode;for(s in w){p=w[s],r=[];for(h=0,t=s.length;h<t;h++)q=s[h],r[h]=s.charCodeAt(h);f=Math.pow(.1,String(p.length).length+1);for(h=0,u=p.length;h<u;h++){o=p[h],d=f*(h+1),c={min:Infinity,max:-Infinity,map:{}};for(i=0,v=o.length;i<v;i++)m=o[i],b=o.charCodeAt(i),b!==32&&(c.map[b]=r[i]+d),b<c.min&&(c.min=b),b>c.max&&(c.max=b);e.encode.push(c)}}x.push(function(){var a,b,d,f,i,j;i=g.long,j=[];for(k in i){c=i[k],l={};for(s in c){p=c[s],r=[];for(h=0,b=s.length;h<b;h++)q=s[h],r[h]=s.charCodeAt(h);for(a=0,d=p.length;a<d;a++){o=p[a];for(h=0,f=o.length;h<f;h++)m=o[h],n=o.charCodeAt(h),n!==32&&(l[n]=r[h])}}j.push(e.long[k.charCodeAt(0)]=l)}return j}())}return x}(),f.dictSort=function(a,b){return b==null&&(b=null),a.sort(function(a,c){return b&&(a=a[b],c=c[b]),d(a,c)}),a},d=function(a,b){var c,d,e,f;d=0;for(e=0,f=Math.min(a.length,b.length);e<f;e+=1){c=h(a,e)-h(b,e);if(c<-0.1||c>.1)return c;d===0&&(d=c)}return(c=a.length-b.length)!==0?c:d},h=function(a,b){var c,d,f,g,h;c=a.charCodeAt(b),b>0&&(g=e.long[c])!=null&&(h=a.charCodeAt(b-1),(d=g[h])!=null&&(c=d)),f=e.encode.length;while(f--){g=e.encode[f];if(c>=g.min&&c<=g.max&&g.map[c]!=null)return g.map[c]}return c},f.natSort=function(a,d){var e,f,h,i,j,k;d==null&&(d=null),i=[];for(h=0,k=a.length;h<k;h++)j=a[h],j=d?j[d]:j,e=[],j.replace(c,function(a,c,d){var f;return f={},c&&(c=c.replace(b,function(a){return"0123456789".indexOf(a)}),f.num=Number(c)),f.str=a,e.push(f)}),i[h]={raw:j,chunks:e};i.sort(g),f=[],h=i.length;while(h--)f[h]=i[h].raw;return f},g=function(a,b){var c,e,f,g,h;a=a.chunks,b=b.chunks,e=0;for(f=0,g=Math.min(a.length,b.length);f<g;f+=1)if(typeof a[f].num!="undefined"&&typeof b[f].num!="undefined"){if((c=a[f].num-b[f].num)!==0)return c;(c=a[f].str.length-b[f].str.length)!==0?e===0&&(e=(h=c<0)!=null?h:-{.1:.1}):e===0&&(e=d(a[f].str,b[f].str))}else{if(typeof a[f].num!="undefined")return d(a[f].str,b[f].str);if(typeof b[f].num!="undefined")return d(a[f].str,b[f].str);c=d(a[f].str,b[f].str);if(c<-0.1||c>.1)return c;e===0&&(e=c)}return c=a.length-b.length,c!==0?c:e},typeof define!="undefined"&&define!==null?define(function(){return f}):typeof window!="undefined"&&window!==null&&(window.mn==null&&(window.mn={}),window.mn.dsk==null&&(window.mn.dsk={}),window.mn.dsk.sorter==null&&(window.mn.dsk.sorter=f))}).call(this)
View
37 lib/node/sorter.js
@@ -1,5 +1,5 @@
(function() {
- var DICTIONARY_DATA, R_MULTIBYTE_NUM, R_NUM_SPLITTER, dictCompare, dictionary, natCompare, natParse, toDictCode;
+ var DICTIONARY_DATA, R_MULTIBYTE_NUM, R_NUM_SPLITTER, dictCompare, dictionary, natCompare, toDictCode;
R_NUM_SPLITTER = /([0-9 \uff10-\uff19\u3000]+)|([^0-9 \uff10-\uff19\u3000]+)/g;
@@ -147,16 +147,28 @@
};
exports.natSort = function(src, key) {
- var dst, i, tmps, v, _len;
+ var chunks, dst, i, tmps, v, _len;
if (key == null) key = null;
tmps = [];
- console.log(src);
for (i = 0, _len = src.length; i < _len; i++) {
v = src[i];
v = key ? v[key] : v;
+ chunks = [];
+ v.replace(R_NUM_SPLITTER, function(matched, num, str) {
+ var chunk;
+ chunk = {};
+ if (num) {
+ num = num.replace(R_MULTIBYTE_NUM, function(matched) {
+ return '0123456789'.indexOf(matched);
+ });
+ chunk.num = Number(num);
+ }
+ chunk.str = matched;
+ return chunks.push(chunk);
+ });
tmps[i] = {
raw: v,
- chunks: natParse(v)
+ chunks: chunks
};
}
tmps.sort(natCompare);
@@ -168,23 +180,6 @@
return dst;
};
- natParse = function(text) {
- var chunks;
- chunks = [];
- text.replace(R_NUM_SPLITTER, function(matched, num, str) {
- var chunk;
- chunk = {};
- if (num) {
- chunk.num = Number(num.replace(R_MULTIBYTE_NUM, function(matched) {
- return '0123456789'.indexOf(matched);
- }));
- }
- chunk.str = matched;
- return chunks.push(chunk);
- });
- return chunks;
- };
-
natCompare = function(a, b) {
var d, delta, i, _ref, _ref2;
a = a.chunks;
View
19 package.json
@@ -1,13 +1,22 @@
{
"name" :"sorter",
- "version" :"0.0.0",
+ "version" :"0.0.1",
"description":"Dictionary and natural sort module.",
"tags" :["dictionary natural sort array"],
- "author" :"Daisuke MINO <daisuke.mino@gmail.com>",
- "license" :"MIT",
- "main" :"./lib/node/sorter",
+ "author" :{
+ "name" :"Daisuke MINO",
+ "email":"daisuke.mino@gmail.com"
+ },
"repository" :{
"type":"git",
"url" :"https://minodisk@github.com/minodisk/sorter-js.git"
- }
+ },
+ "engines":[
+ "node >= 0.6.0"
+ ],
+ "license" :"MIT",
+ "dependencies":{
+ "underscore": ">=1.3.2"
+ },
+ "main" :"./lib/node/sorter"
}
View
39 src/sorter.coffee
@@ -178,12 +178,22 @@ toDictCode = (text, index)->
exports.natSort = (src, key = null)->
tmps = []
- console.log src
for v, i in src
v = if key then v[key] else v
+
+ chunks = []
+ v.replace R_NUM_SPLITTER, (matched, num, str)->
+ chunk = {}
+ if num
+ num = num.replace R_MULTIBYTE_NUM, (matched)->
+ '0123456789'.indexOf matched
+ chunk.num = Number num
+ chunk.str = matched
+ chunks.push chunk
+
tmps[i] =
raw : v
- chunks: natParse v
+ chunks: chunks
tmps.sort(natCompare)
dst = []
i = tmps.length
@@ -191,32 +201,7 @@ exports.natSort = (src, key = null)->
dst[i] = tmps[i].raw
dst
-natParse = (text)->
- chunks = []
- text.replace R_NUM_SPLITTER, (matched, num, str)->
- chunk = {}
- if num
- chunk.num = Number(num.replace R_MULTIBYTE_NUM, (matched)->
- '0123456789'.indexOf(matched)
- )
- chunk.str = matched
- chunks.push(chunk)
- #console.log(text, '->', JSON.stringify(chunks))
- chunks
-
natCompare = (a, b)->
- # if (typeof a.num isnt 'undefined' and typeof b.num isnt 'undefined') {
- # if ((d = a.num - b.num) isnt 0) {
- # return d
- # } else if ((d = a.raw.length - b.raw.length) isnt 0) {
- # return d
- # }
- # return dictionaryCompare(a.raw, b.raw)
- # } else if (typeof a.num isnt 'undefined') {
- # return -1
- # } else if (typeof b.num isnt 'undefined') {
- # return 1
- # }
a = a.chunks
b = b.chunks
View
346 test/test-sorter.coffee
@@ -0,0 +1,346 @@
+sorter = require '../lib/node/sorter'
+_ = require 'underscore'
+
+exports.dictSort =
+
+ 'single-byte':
+
+ 'empty':(test)->
+ test.deepEqual sorter.dictSort(['', '']), ['', '']
+ test.done()
+
+ 'same':(test)->
+ test.deepEqual sorter.dictSort(['x', 'x']), ['x', 'x']
+ test.done()
+
+ 'simple':(test)->
+ test.deepEqual sorter.dictSort(['y', 'x']), ['x', 'y']
+ test.done()
+
+ 'number(basic)': (test)->
+ arr = [
+ '123', '132', '213', '231', '312', '321'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'number': (test)->
+ arr = [
+ '0', '00', '000', '001', '01', '010', '011'
+ '1', '10', '100', '101', '11', '110', '111'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'alphabet': (test)->
+ arr = [
+ 'A', 'a'
+ 'AA', 'Aa', 'aA', 'aa'
+ 'AAA', 'AAa', 'AaA', 'Aaa', 'aAA', 'aAa', 'aaA', 'aaa'
+ 'ab', 'abc', 'b'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'mixed': (test)->
+ arr = [
+ 'A10B1', 'A10B10', 'A10B2', 'A1B1', 'A1B10', 'A1B2'
+ 'A2B1', 'A2B10', 'A2B2'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'filename': (test)->
+ arr = [
+ '1.txt', '10.txt', '100.txt'
+ '2.txt', '20.txt'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'readme': (test)->
+ arr = ['AD', 'after', 'AM', 'BC', 'before', 'PM']
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'multibyte':
+
+ 'number': (test)->
+ arr = [
+ '000', '000', '000', '000'
+ '000', '000', '000', '000'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'alphabet': (test)->
+ arr = [
+ 'AAaa', 'AAaa', 'AaAa', 'AaaA', 'AaAa', 'AaaA'
+ 'AAaa', 'AAaa', 'AaAa', 'AaaA', 'AaAa', 'AaaA'
+ 'aAAa', 'aAaA', 'aAAa', 'aAaA', 'aaAA', 'aaAA'
+ 'aAAa', 'aAaA', 'aAAa', 'aAaA', 'aaAA', 'aaAA'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'japanese aiueo': (test)->
+ arr = [
+ '', '', '', '', '', ''
+ '', '', '', '', '', ''
+ '', '', '', '', '', '', '', ''
+ '', '', '', '', '', ''
+ '', '', '', '', '', ''
+ '', '', '', '', ''
+ '', ''
+ '', ''
+ '', '', ''
+ '', '', ''
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'japanese a': (test)->
+ arr = [
+ 'ぁぁぁ', 'ぁぁァ', 'ぁぁァ'
+ 'ぁァぁ', 'ぁァァ', 'ぁァァ'
+ 'ぁァぁ', 'ぁァァ', 'ぁァァ'
+ 'ァぁぁ', 'ァぁァ', 'ァぁァ'
+ 'ァァぁ', 'ァァァ', 'ァァァ'
+ 'ァァぁ', 'ァァァ', 'ァァァ'
+ 'ァぁぁ', 'ァぁァ', 'ァぁァ'
+ 'ァァぁ', 'ァァァ', 'ァァァ'
+ 'ァァぁ', 'ァァァ', 'ァァァ'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'complex': (test)->
+ arr = [
+ 'ァ1'
+ 'あ1'
+ 'ぁ2'
+ 'ァ2'
+ 'ぁァァ00アaaAA'
+ 'ぁァァ00アaaAAア'
+ 'ぁァァ00アaaAAアあ'
+ 'ァァぁ00アAaAあaア'
+ 'ァァぁ00あaアAアAa'
+ 'ァァぁAaA0aあ0アア'
+ 'ァぁァAaAア0aあア0'
+ 'ぁァァあa00aアAAア'
+ 'ァぁァあA0アa0aアA'
+ 'ァァぁアaAaあ0ア0A'
+ 'ぁァァあAaアA0ア0a'
+ 'い234'
+ 'い234'
+ 'イ234'
+ 'ぃ5'
+ 'い5'
+ 'う22'
+ 'う22'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'web':
+
+ 'http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=27978&forum=26': (test)->
+ arr = [
+ 'こじま'
+ 'ごとう'
+ 'こばやし'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'http://www.softvision.co.jp/dbpro/help/guide/jishoord.htm': (test)->
+ arr = [
+ 'かっこ'
+ 'かつご'
+ 'がっこ'
+ 'かっこう'
+ 'かつごう'
+ 'がっこう'
+ 'かっこうあざみ'
+ 'がっこうちゅう'
+ 'カッコー'
+ 'かっこく'
+ ]
+ test.deepEqual sorter.dictSort(_.shuffle(arr)), arr
+ test.done()
+
+exports.natSort =
+
+ 'single byte':
+
+ 'empty':(test)->
+ test.deepEqual sorter.natSort(['', '']), ['', '']
+ test.done()
+
+ 'same':(test)->
+ test.deepEqual sorter.natSort(['x', 'x']), ['x', 'x']
+ test.done()
+
+ 'simple':(test)->
+ test.deepEqual sorter.natSort(['y', 'x']), ['x', 'y']
+ test.done()
+
+ 'ordered two gorups':(test)->
+ test.deepEqual sorter.natSort(['x2', 'x1']), ['x1', 'x2']
+ test.done()
+
+ 'ordered two gorups separated':(test)->
+ test.deepEqual sorter.natSort(['x_2', 'x_1']), ['x_1', 'x_2']
+ test.done()
+
+ 'ordered two gorups separated different distances':(test)->
+ test.deepEqual sorter.natSort(['x_10', 'x_5']), ['x_5', 'x_10']
+ test.done()
+
+ 'php':
+
+ 'negative numbers': (test)->
+ arr = ['-2', '-5', '-1000','0','1','3','9']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'zero padding': (test)->
+ arr = ['0', '8', '009', '09', '10', '011']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'filename starts with number': (test)->
+ arr = ['1.txt', '2.txt', '10.txt', '20.txt', '100.txt']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'filename contains number':(test)->
+ arr = ['img1.png', 'img2.png', 'img10.png', 'img12.png']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'sourcefrog.net':
+
+ 'rfc':(test)->
+ arr = ['rfc1.txt', 'rfc822.txt', 'rfc2086.txt']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ '1':(test)->
+ arr = ['a', 'a0', 'a1', 'a1a', 'a1b', 'a2', 'a10', 'a20']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ '2':(test)->
+ arr = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'decimal':(test)->
+ arr = ['1.001', '1.1', '1.002', '1.02', '1.3', '1.010']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+ 'readme': (test)->
+ arr = ['image_1.jpg', 'image_04.jpg', 'image_4.jpg', 'image_005.jpg', 'image_12.jpg', 'image_21.jpg']
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+
+###
+ 'version': (test)->
+ arr = [
+ '0.0.1', '0.0.2', '0.0.14'
+ '0.3.0', '0.3.00'
+ '0.3.1', '0.3.2', '0.3.02', '0.3.3', '0.3.12', '0.3.020', '0.3.21'
+ '1.0.0', '1.0.1', '1.0.10', '1.0.100', '1.1.0', '1.001.1', '1.2.0'
+ ]
+ test.deepEqual sorter.natSort(_.shuffle(arr)), arr
+ test.done()
+ 'web':
+
+ 'http://sourcefrog.net/projects/natsort/':
+
+ 'raw': (test)->
+ arr
+ arr = ['rfc1.txt', 'rfc822.txt', 'rfc2086.txt']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['a', 'a0', 'a1', 'a1a', 'a1b', 'a2', 'a10', 'a20']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['1.001', '1.002', '1.010', '1.02', '1.1', '1.3']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ test.done()
+
+ 'multibyte': (test)->
+ arr = [
+ 'あーるえふしー1.txt', 'アぁルエフシぃ1.txt'
+ 'ああるえふしぃ822.txt', 'ああるえふしぃ822.txt', 'ああるえふしぃ822.txt', 'ああるえふしー822.txt'
+ 'あーるえふしー2086.txt', 'あーるえふしー2086.txt', 'あーるえふしー2086.txt', 'あーるえふしー2086.txt'
+ 'あーるえふしー2086.txt', 'ぁぃ'
+ ]
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['あ', 'ぁ0', 'ァ1', 'ぁ1ア', 'ァ1い', 'ァ2', 'ぁ10', 'あ20']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['1.001', '1.002', '1.010', '1.02', '1.1', '1.3']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ test.done()
+
+ 'complex': (test)->
+ arr = [
+ '1.001', '1.002', '1.010', '1.02', '1.1', '1.3'
+ 'a', 'a0', 'a1', 'a1a', 'a1b', 'a2', 'a10', 'a20'
+ 'rfc1.txt', 'rfc822.txt', 'rfc2086.txt'
+ 'x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'
+ ]
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ test.done()
+
+ 'http://rubygems.org/gems/naturalsort': (test)->
+ arr = ['a1', 'a2', 'a11', 'a12','a21']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['A', 'a', 'B', 'b', 'C', 'c']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['x_1', 'x__2']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ arr = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ test.done()
+
+ 'http://sourcefrog.net/projects/natsort/example-out.txt': (test)->
+ arr = [
+ '1-2'
+ '1-02'
+ '1-20'
+ '10-20'
+ 'fred'
+ 'jane'
+ 'pic01'
+ 'pic2'
+ 'pic02'
+ 'pic02a'
+ 'pic3'
+ 'pic4'
+ 'pic 4 else'
+ 'pic 5'
+ 'pic05'
+ 'pic 5 '
+ 'pic 5 something'
+ 'pic 6'
+ 'pic 7'
+ 'pic100'
+ 'pic100a'
+ 'pic120'
+ 'pic121'
+ 'pic02000'
+ 'tom'
+ 'x2-g8'
+ 'x2-y7'
+ 'x2-y08'
+ 'x8-y8'
+ ]
+ test.deepEqual(sorter.natSort(__.shuffle(arr)), arr)
+ test.done()
+###
View
364 test/test.coffee
@@ -1,364 +0,0 @@
-sorter = require '../lib/node/sorter'
-
-_shuffle = (src)->
- src = src.slice()
- i = src.length
- while i
- j = Math.random() * i >> 0
- v = src[--i]
- src[i] = src[j]
- src[j] = v
- src
-
-exports.dictionary =
-
- 'empty':(test)->
- src = ['', '']
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'single-byte':
-
- 'number(basic)': (test)->
- src = [
- '123', '132', '213', '231', '312', '321'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'number': (test)->
- src = [
- '0', '00', '000', '001', '01', '010', '011'
- '1', '10', '100', '101', '11', '110', '111'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'alphabet': (test)->
- src = [
- 'A', 'a'
- 'AA', 'Aa', 'aA', 'aa'
- 'AAA', 'AAa', 'AaA', 'Aaa', 'aAA', 'aAa', 'aaA', 'aaa'
- 'ab', 'abc', 'b'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'mixed': (test)->
- src = [
- 'A10B1', 'A10B10', 'A10B2', 'A1B1', 'A1B10', 'A1B2'
- 'A2B1', 'A2B10', 'A2B2'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'filename': (test)->
- src = [
- '1.txt', '10.txt', '100.txt'
- '2.txt', '20.txt'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'multibyte':
-
- 'number': (test)->
- src = [
- '000', '000', '000', '000'
- '000', '000', '000', '000'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'alphabet': (test)->
- src = [
- 'AAaa', 'AAaa', 'AaAa', 'AaaA', 'AaAa', 'AaaA'
- 'AAaa', 'AAaa', 'AaAa', 'AaaA', 'AaAa', 'AaaA'
- 'aAAa', 'aAaA', 'aAAa', 'aAaA', 'aaAA', 'aaAA'
- 'aAAa', 'aAaA', 'aAAa', 'aAaA', 'aaAA', 'aaAA'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'japanese aiueo': (test)->
- src = [
- '', '', '', '', '', ''
- '', '', '', '', '', ''
- '', '', '', '', '', '', '', ''
- '', '', '', '', '', ''
- '', '', '', '', '', ''
- '', '', '', '', ''
- '', ''
- '', ''
- '', '', ''
- '', '', ''
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'japanese a': (test)->
- src = [
- 'ぁぁぁ', 'ぁぁァ', 'ぁぁァ'
- 'ぁァぁ', 'ぁァァ', 'ぁァァ'
- 'ぁァぁ', 'ぁァァ', 'ぁァァ'
- 'ァぁぁ', 'ァぁァ', 'ァぁァ'
- 'ァァぁ', 'ァァァ', 'ァァァ'
- 'ァァぁ', 'ァァァ', 'ァァァ'
- 'ァぁぁ', 'ァぁァ', 'ァぁァ'
- 'ァァぁ', 'ァァァ', 'ァァァ'
- 'ァァぁ', 'ァァァ', 'ァァァ'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'complex': (test)->
- src = [
- 'ァ1'
- 'あ1'
- 'ぁ2'
- 'ァ2'
- 'ぁァァ00アaaAA'
- 'ぁァァ00アaaAAア'
- 'ぁァァ00アaaAAアあ'
- 'ァァぁ00アAaAあaア'
- 'ァァぁ00あaアAアAa'
- 'ァァぁAaA0aあ0アア'
- 'ァぁァAaAア0aあア0'
- 'ぁァァあa00aアAAア'
- 'ァぁァあA0アa0aアA'
- 'ァァぁアaAaあ0ア0A'
- 'ぁァァあAaアA0ア0a'
- 'い234'
- 'い234'
- 'イ234'
- 'ぃ5'
- 'い5'
- 'う22'
- 'う22'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'web':
-
- 'http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=27978&forum=26': (test)->
- src = [
- 'こじま'
- 'ごとう'
- 'こばやし'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
- 'http://www.softvision.co.jp/dbpro/help/guide/jishoord.htm': (test)->
- src = [
- 'かっこ'
- 'かつご'
- 'がっこ'
- 'かっこう'
- 'かつごう'
- 'がっこう'
- 'かっこうあざみ'
- 'がっこうちゅう'
- 'カッコー'
- 'かっこく'
- ]
- test.deepEqual sorter.dictionary(_shuffle(src)), src
- test.done()
-
-###
-exports.naturalSort =
-
- 'empty':(test)->
- test.deepEqual sorter.naturalSort(['', '']), ['', '']
- test.done()
-
- 'identical simple':(test)->
- test.deepEqual sorter.naturalSort(['x1', 'x1']), ['x1', 'x1']
- test.done()
-
- 'ordered simple':(test)->
- test.deepEqual sorter.naturalSort(['y', 'x']), ['x', 'y']
- test.done()
-
- 'ordered two gorups':(test)->
- test.deepEqual sorter.naturalSort(['x2', 'x1']), ['x1', 'x2']
- test.done()
-
- 'ordered two gorups separated':(test)->
- test.deepEqual sorter.naturalSort(['x_2', 'x_1']), ['x_1', 'x_2']
- test.done()
-
- 'ordered two gorups separated different distances':(test)->
- test.deepEqual sorter.naturalSort(['x__2', 'x_1']), ['x_1', 'x__2']
- test.done()
-
- 'ordered two gorups separated different distances':(test)->
- test.deepEqual sorter.naturalSort(['x_2', 'x__1']), ['x__1', 'x_2']
- test.done()
-
- 'three groups':(test)->
- _testNaturalSort test
- , ['hello 2 world', 'hello world', 'hello world 2']
- , ['hello world', 'hello world 2', 'hello 2 world']
-
- 'test_multiple_string_number':(test)->
- test.deepEqual ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'], ['x2-y08', 'x8-y8', 'x2-y7', 'x2-g8']
-
- 'test_multiple_string_number_2':(test)->
- test.deepEqual ['x02-g8', 'x2-y7', 'x02-y08', 'x8-y8'], ['x02-y08', 'x8-y8', 'x2-y7', 'x02-g8']
-
- 'test_filename':(test)->
- test.deepEqual ["img1.png", "img2.png", "img10.png", "img12.png"], ["img12.png", "img10.png", "img2.png", "img1.png"]
-
- 'single-byte':
-
- 'number(basic)': (test)->
- src = [
- '123', '132', '213', '231', '312', '321'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'number': (test)->
- src = [
- '0', '00', '000', '1', '01', '001', '10', '010'
- '11', '011', '100', '101', '110', '111'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'number(decimal)': (test)->
- src = [
- '.0', '.00', '0', '00', '000', '0.', '00.', '0.0'
- '1', '01', '001', '0001', '1.', '001.', '1.0', '01.0', '1.00'
- '10', '010', '00010', '10.', '0010.', '10.0', '010.0', '10.00'
- '11', '011', '000000000000000000000000000000000000011', '11.0'
- '100', '101', '109.99999', '110', '0000000110', '110.00001', '111'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'alphabet': (test)->
- src = [
- 'A', 'a'
- 'AA', 'Aa', 'aA', 'aa'
- 'AAA', 'AAa', 'AaA', 'Aaa', 'aAA', 'aAa', 'aaA', 'aaa'
- 'ab', 'abc', 'b'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'mixed': (test)->
- src = [
- '0', '1', '1.1'
- 'a0', 'a00', 'a000'
- 'a1', 'a01', 'a001'
- 'a1.1', 'a01.1', 'a001.1'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'filename': (test)->
- src = ['1.txt', '2.txt', '10.txt', '20.txt', '100.txt']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'version': (test)->
- src = [
- '0.0.1', '0.0.2', '0.0.14'
- '0.3.0', '0.3.00'
- '0.3.1', '0.3.2', '0.3.02', '0.3.3', '0.3.12', '0.3.020', '0.3.21'
- '1.0.0', '1.0.1', '1.0.10', '1.0.100', '1.1.0', '1.001.1', '1.2.0'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'web':
-
- 'http://sourcefrog.net/projects/natsort/':
-
- 'raw': (test)->
- src
- src = ['rfc1.txt', 'rfc822.txt', 'rfc2086.txt']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['a', 'a0', 'a1', 'a1a', 'a1b', 'a2', 'a10', 'a20']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['1.001', '1.002', '1.010', '1.02', '1.1', '1.3']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'multibyte': (test)->
- src = [
- 'あーるえふしー1.txt', 'アぁルエフシぃ1.txt'
- 'ああるえふしぃ822.txt', 'ああるえふしぃ822.txt', 'ああるえふしぃ822.txt', 'ああるえふしー822.txt'
- 'あーるえふしー2086.txt', 'あーるえふしー2086.txt', 'あーるえふしー2086.txt', 'あーるえふしー2086.txt'
- 'あーるえふしー2086.txt', 'ぁぃ'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['あ', 'ぁ0', 'ァ1', 'ぁ1ア', 'ァ1い', 'ァ2', 'ぁ10', 'あ20']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['1.001', '1.002', '1.010', '1.02', '1.1', '1.3']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'complex': (test)->
- src = [
- '1.001', '1.002', '1.010', '1.02', '1.1', '1.3'
- 'a', 'a0', 'a1', 'a1a', 'a1b', 'a2', 'a10', 'a20'
- 'rfc1.txt', 'rfc822.txt', 'rfc2086.txt'
- 'x2-g8', 'x2-y7', 'x2-y08', 'x8-y8'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'http://rubygems.org/gems/naturalsort': (test)->
- src = ['a1', 'a2', 'a11', 'a12','a21']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['A', 'a', 'B', 'b', 'C', 'c']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['x_1', 'x__2']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- src = ['x2-g8', 'x2-y7', 'x2-y08', 'x8-y8']
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-
- 'http://sourcefrog.net/projects/natsort/example-out.txt': (test)->
- src = [
- '1-2'
- '1-02'
- '1-20'
- '10-20'
- 'fred'
- 'jane'
- 'pic01'
- 'pic2'
- 'pic02'
- 'pic02a'
- 'pic3'
- 'pic4'
- 'pic 4 else'
- 'pic 5'
- 'pic05'
- 'pic 5 '
- 'pic 5 something'
- 'pic 6'
- 'pic 7'
- 'pic100'
- 'pic100a'
- 'pic120'
- 'pic121'
- 'pic02000'
- 'tom'
- 'x2-g8'
- 'x2-y7'
- 'x2-y08'
- 'x8-y8'
- ]
- test.deepEqual(arrayutil.naturalSort(_shuffle(src)), src)
- test.done()
-###

0 comments on commit 716cb36

Please sign in to comment.
Something went wrong with that request. Please try again.