Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

v0.0.1

  • Loading branch information...
commit e6d4d1341ec03dd47058ea9d68c83a6bc5ea12bf 1 parent a1abfd1
@minodisk authored
View
106 lib/browser/sorter.js
@@ -8,13 +8,13 @@
DICTIONARY_DATA = {
en: {
encode: {
- 'A': ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
- '0': ['0123456789']
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
+ '01234567890': ['0123456789']
}
},
ja: {
encode: {
- '': ['ぁぃぅぇぉ っ ゃ ゅ ょ ゎ', 'ァィゥェォヵ ヶ ッ ャ ュ ョ ヮ', 'ァィゥェォ ッ ャ ュ ョ', 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん', 'アイウエオカキクケコサシスセソタチツデトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワヰ ヱヲン', 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワ ヲン', ' ゔ がぎぐげござじずぜぞだぢづでど ばびぶべぼ', ' ヴ ガギグゲゴザジズゼゾダヂヅデド バビブベボ', ' ぱぴぷぺぽ', ' パピプペポ']
+ 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん': ['ぁぃぅぇぉ っ ゃ ゅ ょ ゎ', 'ァィゥェォヵ ヶ ッ ャ ュ ョ ヮ', 'ァィゥェォ ッ ャ ュ ョ', 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん', 'アイウエオカキクケコサシスセソタチツデトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワヰ ヱヲン', 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワ ヲン', ' ゔ がぎぐげござじずぜぞだぢづでど ばびぶべぼ', ' ヴ ガギグゲゴザジズゼゾダヂヅデド バビブベボ', ' ぱぴぷぺぽ', ' パピプペポ']
},
long: {
'': {
@@ -35,40 +35,39 @@
};
(function() {
- var baseCode, code, data, delta, dictionaryData, i, j, language, longChar, map, srcChar, srcCode, srcText, srcTexts, trgChar, trgCode, trgCodes, trgText, _len, _ref, _results;
+ var code, data, delta, deltaPerRow, dictionaryData, i, j, language, longChar, map, srcChar, srcCode, srcText, srcTexts, trgChar, trgCodes, trgText, _len, _len2, _len3, _ref, _results;
_results = [];
for (language in DICTIONARY_DATA) {
dictionaryData = DICTIONARY_DATA[language];
_ref = dictionaryData.encode;
- for (trgChar in _ref) {
- srcTexts = _ref[trgChar];
- trgCode = trgChar.charCodeAt(0);
- delta = Math.pow(0.1, String(srcTexts.length).length + 1);
- for (i = 0, _len = srcTexts.length; i < _len; i++) {
+ for (trgText in _ref) {
+ srcTexts = _ref[trgText];
+ trgCodes = [];
+ for (i = 0, _len = trgText.length; i < _len; i++) {
+ trgChar = trgText[i];
+ trgCodes[i] = trgText.charCodeAt(i);
+ }
+ deltaPerRow = Math.pow(0.1, String(srcTexts.length).length + 1);
+ for (i = 0, _len2 = srcTexts.length; i < _len2; i++) {
srcText = srcTexts[i];
- baseCode = trgCode + delta * (i + 1);
+ delta = deltaPerRow * (i + 1);
data = {
- trgChar: trgChar,
- trgCode: trgCode,
- src: srcText,
- codeMap: {}
+ min: Infinity,
+ max: -Infinity,
+ map: {}
};
- data.min = Number.MAX_VALUE;
- data.max = Number.MIN_VALUE;
- j = srcText.length;
- while (j--) {
+ for (j = 0, _len3 = srcText.length; j < _len3; j++) {
+ srcChar = srcText[j];
code = srcText.charCodeAt(j);
- if (code !== 32) {
- data.codeMap[code] = baseCode + j;
- if (code < data.min) data.min = code;
- if (code > data.max) data.max = code;
- }
+ if (code !== 32) data.map[code] = trgCodes[j] + delta;
+ if (code < data.min) data.min = code;
+ if (code > data.max) data.max = code;
}
dictionary.encode.push(data);
}
}
_results.push((function() {
- var _i, _len2, _len3, _len4, _ref2, _results2;
+ var _i, _len4, _len5, _len6, _ref2, _results2;
_ref2 = dictionaryData.long;
_results2 = [];
for (longChar in _ref2) {
@@ -77,13 +76,13 @@
for (trgText in data) {
srcTexts = data[trgText];
trgCodes = [];
- for (i = 0, _len2 = trgText.length; i < _len2; i++) {
+ for (i = 0, _len4 = trgText.length; i < _len4; i++) {
trgChar = trgText[i];
trgCodes[i] = trgText.charCodeAt(i);
}
- for (_i = 0, _len3 = srcTexts.length; _i < _len3; _i++) {
+ for (_i = 0, _len5 = srcTexts.length; _i < _len5; _i++) {
srcText = srcTexts[_i];
- for (i = 0, _len4 = srcText.length; i < _len4; i++) {
+ for (i = 0, _len6 = srcText.length; i < _len6; i++) {
srcChar = srcText[i];
srcCode = srcText.charCodeAt(i);
if (srcCode !== 32) map[srcCode] = trgCodes[i];
@@ -98,8 +97,6 @@
return _results;
})();
- console.log(dictionary);
-
exports = {};
exports.dictionary = function(src, key) {
@@ -122,8 +119,7 @@
if (d < -0.1 || d > 0.1) return d;
if (delta === 0) delta = d;
}
- d = a.length - b.length;
- if (d !== 0) {
+ if ((d = a.length - b.length) !== 0) {
return d;
} else {
return delta;
@@ -140,13 +136,35 @@
i = dictionary.encode.length;
while (i--) {
map = dictionary.encode[i];
- if (code >= map.min && code <= map.max && map.codeMap[code]) {
- return map.codeMap[code];
+ if (code >= map.min && code <= map.max && (map.map[code] != null)) {
+ return map.map[code];
}
}
return code;
};
+ exports.naturalSort = function(src, key) {
+ var 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;
+ tmps[i] = {
+ raw: v,
+ chunks: naturalParse(v)
+ };
+ }
+ tmps.sort(naturalCompare);
+ dst = [];
+ i = tmps.length;
+ while (i--) {
+ dst[i] = tmps[i].raw;
+ }
+ return dst;
+ };
+
naturalParse = function(text) {
var chunks;
chunks = [];
@@ -200,28 +218,6 @@
}
};
- exports.naturalSort = function(src, key) {
- var 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;
- tmps[i] = {
- raw: v,
- chunks: naturalParse(v)
- };
- }
- tmps.sort(naturalCompare);
- dst = [];
- i = tmps.length;
- while (i--) {
- dst[i] = tmps[i].raw;
- }
- return dst;
- };
-
if (typeof define !== "undefined" && define !== null) {
define(function() {
return exports;
View
2  lib/browser/sorter.min.js
@@ -1 +1 @@
-((function(){var a,b,c,d,e,f,g,h,i;c=/([0-9 \uff10-\uff19\u3000]+)|([^0-9 \uff10-\uff19\u3000]+)/g,b=/[\uff10-\uff19]/g,a={en:{encode:{A:["ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz","abcdefghijklmnopqrstuvwxyz"],0:["0123456789"]}},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;w=[];for(j in a){g=a[j],v=g.encode;for(q in v){p=v[q],r=q.charCodeAt(0),f=Math.pow(.1,String(p.length).length+1);for(h=0,u=p.length;h<u;h++){o=p[h],b=r+f*(h+1),d={trgChar:q,trgCode:r,src:o,codeMap:{}},d.min=Number.MAX_VALUE,d.max=Number.MIN_VALUE,i=o.length;while(i--)c=o.charCodeAt(i),c!==32&&(d.codeMap[c]=b+i,c<d.min&&(d.min=c),c>d.max&&(d.max=c));e.encode.push(d)}}w.push(function(){var a,b,c,f,i,j;i=g.long,j=[];for(k in i){d=i[k],l={};for(t in d){p=d[t],s=[];for(h=0,b=t.length;h<b;h++)q=t[h],s[h]=t.charCodeAt(h);for(a=0,c=p.length;a<c;a++){o=p[a];for(h=0,f=o.length;h<f;h++)m=o[h],n=o.charCodeAt(h),n!==32&&(l[n]=s[h])}}j.push(e.long[k.charCodeAt(0)]=l)}return j}())}return w}(),console.log(e),f={},f.dictionary=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=i(a,e)-i(b,e);if(c<-0.1||c>.1)return c;d===0&&(d=c)}return c=a.length-b.length,c!==0?c:d},i=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.codeMap[c])return g.codeMap[c]}return c},h=function(a){var d;return d=[],a.replace(c,function(a,c,e){var f;return f={},c&&(f.num=Number(c.replace(b,function(a){return"0123456789".indexOf(a)}))),f.str=a,d.push(f)}),d},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},f.naturalSort=function(a,b){var c,d,e,f,i;b==null&&(b=null),e=[],console.log(a);for(d=0,i=a.length;d<i;d++)f=a[d],f=b?f[b]:f,e[d]={raw:f,chunks:h(f)};e.sort(g),c=[],d=e.length;while(d--)c[d]=e[d].raw;return c},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)
+((function(){var a,b,c,d,e,f,g,h,i;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"]}},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={},f.dictionary=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=i(a,e)-i(b,e);if(c<-0.1||c>.1)return c;d===0&&(d=c)}return(c=a.length-b.length)!==0?c:d},i=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.naturalSort=function(a,b){var c,d,e,f,i;b==null&&(b=null),e=[],console.log(a);for(d=0,i=a.length;d<i;d++)f=a[d],f=b?f[b]:f,e[d]={raw:f,chunks:h(f)};e.sort(g),c=[],d=e.length;while(d--)c[d]=e[d].raw;return c},h=function(a){var d;return d=[],a.replace(c,function(a,c,e){var f;return f={},c&&(f.num=Number(c.replace(b,function(a){return"0123456789".indexOf(a)}))),f.str=a,d.push(f)}),d},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
106 lib/node/sorter.js
@@ -8,13 +8,13 @@
DICTIONARY_DATA = {
en: {
encode: {
- 'A': ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
- '0': ['0123456789']
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
+ '01234567890': ['0123456789']
}
},
ja: {
encode: {
- '': ['ぁぃぅぇぉ っ ゃ ゅ ょ ゎ', 'ァィゥェォヵ ヶ ッ ャ ュ ョ ヮ', 'ァィゥェォ ッ ャ ュ ョ', 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん', 'アイウエオカキクケコサシスセソタチツデトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワヰ ヱヲン', 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワ ヲン', ' ゔ がぎぐげござじずぜぞだぢづでど ばびぶべぼ', ' ヴ ガギグゲゴザジズゼゾダヂヅデド バビブベボ', ' ぱぴぷぺぽ', ' パピプペポ']
+ 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん': ['ぁぃぅぇぉ っ ゃ ゅ ょ ゎ', 'ァィゥェォヵ ヶ ッ ャ ュ ョ ヮ', 'ァィゥェォ ッ ャ ュ ョ', 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん', 'アイウエオカキクケコサシスセソタチツデトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワヰ ヱヲン', 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤ ユ ヨラリルレロワ ヲン', ' ゔ がぎぐげござじずぜぞだぢづでど ばびぶべぼ', ' ヴ ガギグゲゴザジズゼゾダヂヅデド バビブベボ', ' ぱぴぷぺぽ', ' パピプペポ']
},
long: {
'': {
@@ -35,40 +35,39 @@
};
(function() {
- var baseCode, code, data, delta, dictionaryData, i, j, language, longChar, map, srcChar, srcCode, srcText, srcTexts, trgChar, trgCode, trgCodes, trgText, _len, _ref, _results;
+ var code, data, delta, deltaPerRow, dictionaryData, i, j, language, longChar, map, srcChar, srcCode, srcText, srcTexts, trgChar, trgCodes, trgText, _len, _len2, _len3, _ref, _results;
_results = [];
for (language in DICTIONARY_DATA) {
dictionaryData = DICTIONARY_DATA[language];
_ref = dictionaryData.encode;
- for (trgChar in _ref) {
- srcTexts = _ref[trgChar];
- trgCode = trgChar.charCodeAt(0);
- delta = Math.pow(0.1, String(srcTexts.length).length + 1);
- for (i = 0, _len = srcTexts.length; i < _len; i++) {
+ for (trgText in _ref) {
+ srcTexts = _ref[trgText];
+ trgCodes = [];
+ for (i = 0, _len = trgText.length; i < _len; i++) {
+ trgChar = trgText[i];
+ trgCodes[i] = trgText.charCodeAt(i);
+ }
+ deltaPerRow = Math.pow(0.1, String(srcTexts.length).length + 1);
+ for (i = 0, _len2 = srcTexts.length; i < _len2; i++) {
srcText = srcTexts[i];
- baseCode = trgCode + delta * (i + 1);
+ delta = deltaPerRow * (i + 1);
data = {
- trgChar: trgChar,
- trgCode: trgCode,
- src: srcText,
- codeMap: {}
+ min: Infinity,
+ max: -Infinity,
+ map: {}
};
- data.min = Number.MAX_VALUE;
- data.max = Number.MIN_VALUE;
- j = srcText.length;
- while (j--) {
+ for (j = 0, _len3 = srcText.length; j < _len3; j++) {
+ srcChar = srcText[j];
code = srcText.charCodeAt(j);
- if (code !== 32) {
- data.codeMap[code] = baseCode + j;
- if (code < data.min) data.min = code;
- if (code > data.max) data.max = code;
- }
+ if (code !== 32) data.map[code] = trgCodes[j] + delta;
+ if (code < data.min) data.min = code;
+ if (code > data.max) data.max = code;
}
dictionary.encode.push(data);
}
}
_results.push((function() {
- var _i, _len2, _len3, _len4, _ref2, _results2;
+ var _i, _len4, _len5, _len6, _ref2, _results2;
_ref2 = dictionaryData.long;
_results2 = [];
for (longChar in _ref2) {
@@ -77,13 +76,13 @@
for (trgText in data) {
srcTexts = data[trgText];
trgCodes = [];
- for (i = 0, _len2 = trgText.length; i < _len2; i++) {
+ for (i = 0, _len4 = trgText.length; i < _len4; i++) {
trgChar = trgText[i];
trgCodes[i] = trgText.charCodeAt(i);
}
- for (_i = 0, _len3 = srcTexts.length; _i < _len3; _i++) {
+ for (_i = 0, _len5 = srcTexts.length; _i < _len5; _i++) {
srcText = srcTexts[_i];
- for (i = 0, _len4 = srcText.length; i < _len4; i++) {
+ for (i = 0, _len6 = srcText.length; i < _len6; i++) {
srcChar = srcText[i];
srcCode = srcText.charCodeAt(i);
if (srcCode !== 32) map[srcCode] = trgCodes[i];
@@ -98,8 +97,6 @@
return _results;
})();
- console.log(dictionary);
-
exports = {};
exports.dictionary = function(src, key) {
@@ -122,8 +119,7 @@
if (d < -0.1 || d > 0.1) return d;
if (delta === 0) delta = d;
}
- d = a.length - b.length;
- if (d !== 0) {
+ if ((d = a.length - b.length) !== 0) {
return d;
} else {
return delta;
@@ -140,13 +136,35 @@
i = dictionary.encode.length;
while (i--) {
map = dictionary.encode[i];
- if (code >= map.min && code <= map.max && map.codeMap[code]) {
- return map.codeMap[code];
+ if (code >= map.min && code <= map.max && (map.map[code] != null)) {
+ return map.map[code];
}
}
return code;
};
+ exports.naturalSort = function(src, key) {
+ var 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;
+ tmps[i] = {
+ raw: v,
+ chunks: naturalParse(v)
+ };
+ }
+ tmps.sort(naturalCompare);
+ dst = [];
+ i = tmps.length;
+ while (i--) {
+ dst[i] = tmps[i].raw;
+ }
+ return dst;
+ };
+
naturalParse = function(text) {
var chunks;
chunks = [];
@@ -200,28 +218,6 @@
}
};
- exports.naturalSort = function(src, key) {
- var 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;
- tmps[i] = {
- raw: v,
- chunks: naturalParse(v)
- };
- }
- tmps.sort(naturalCompare);
- dst = [];
- i = tmps.length;
- while (i--) {
- dst[i] = tmps[i].raw;
- }
- return dst;
- };
-
module.exports = exports;
}).call(this);
View
81 src/sorter.coffee
@@ -3,17 +3,17 @@ R_MULTIBYTE_NUM = /[\uff10-\uff19]/g
DICTIONARY_DATA =
en:
encode:
- 'A': [
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': [
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'abcdefghijklmnopqrstuvwxyz'
]
- '0': [
+ '01234567890': [
'0123456789'
]
ja:
encode:
- '': [
+ 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもや ゆ よらりるれろわゐ ゑをん': [
'ぁぃぅぇぉ っ ゃ ゅ ょ ゎ'
'ァィゥェォヵ ヶ ッ ャ ュ ョ ヮ'
'ァィゥェォ ッ ャ ュ ョ'
@@ -94,31 +94,29 @@ dictionary =
do ->
for language, dictionaryData of DICTIONARY_DATA
- # encode
- for trgChar, srcTexts of dictionaryData.encode
- trgCode = trgChar.charCodeAt 0
- delta = Math.pow 0.1, String(srcTexts.length).length + 1
+ # Setup encode data.
+ for trgText, srcTexts of dictionaryData.encode
+ trgCodes = []
+ for trgChar, i in trgText
+ trgCodes[i] = trgText.charCodeAt i
+ deltaPerRow = Math.pow 0.1, String(srcTexts.length).length + 1
for srcText, i in srcTexts
- baseCode = trgCode + delta * (i + 1)
+ delta = deltaPerRow * (i + 1)
data =
- trgChar: trgChar
- trgCode: trgCode
- src : srcText
- codeMap: {}
- data.min = Number.MAX_VALUE
- data.max = Number.MIN_VALUE
- j = srcText.length
- while j--
+ min: Infinity
+ max: -Infinity
+ map: {}
+ for srcChar, j in srcText
code = srcText.charCodeAt j
if code isnt 32
- data.codeMap[code] = baseCode + j
- if code < data.min
- data.min = code
- if code > data.max
- data.max = code
+ data.map[code] = trgCodes[j] + delta
+ if code < data.min
+ data.min = code
+ if code > data.max
+ data.max = code
dictionary.encode.push data
- # long
+ # Setup long data.
for longChar, data of dictionaryData.long
map = {}
for trgText, srcTexts of data
@@ -132,7 +130,7 @@ do ->
map[srcCode] = trgCodes[i]
dictionary.long[longChar.charCodeAt 0] = map
-console.log dictionary
+#console.log dictionary
exports = {}
@@ -152,8 +150,7 @@ compareAsDictionary = (a, b)->
return d
if delta is 0
delta = d
- d = a.length - b.length
- if d isnt 0
+ if (d = a.length - b.length) isnt 0
d
else
delta
@@ -170,10 +167,25 @@ toDictionaryCode = (text, index)->
i = dictionary.encode.length
while i--
map = dictionary.encode[i]
- if code >= map.min and code <= map.max and map.codeMap[code]
- return map.codeMap[code]
+ if code >= map.min and code <= map.max and map.map[code]?
+ return map.map[code]
code
+exports.naturalSort = (src, key = null)->
+ tmps = []
+ console.log src
+ for v, i in src
+ v = if key then v[key] else v
+ tmps[i] =
+ raw : v
+ chunks: naturalParse v
+ tmps.sort(naturalCompare)
+ dst = []
+ i = tmps.length
+ while i--
+ dst[i] = tmps[i].raw
+ dst
+
naturalParse = (text)->
chunks = []
text.replace R_NUM_SPLITTER, (matched, num, str)->
@@ -239,21 +251,6 @@ naturalCompare = (a, b)->
#console.log(' decide as byte difference', delta)
delta
-exports.naturalSort = (src, key = null)->
- tmps = []
- console.log src
- for v, i in src
- v = if key then v[key] else v
- tmps[i] =
- raw : v
- chunks: naturalParse v
- tmps.sort(naturalCompare)
- dst = []
- i = tmps.length
- while i--
- dst[i] = tmps[i].raw
- dst
-
#if BROWSER
if define?
define -> exports
View
3  test/test.coffee
@@ -166,7 +166,7 @@ exports.dictionary =
test.deepEqual sorter.dictionary(_shuffle(src)), src
test.done()
-
+###
exports.naturalSort =
'empty':(test)->
@@ -197,7 +197,6 @@ exports.naturalSort =
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']
Please sign in to comment.
Something went wrong with that request. Please try again.