Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7ab1a02
Showing
5 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
node_modules/* | ||
npm_debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
exports.clone = | ||
function (ary) { | ||
return ary.map(function (e) { | ||
return Array.isArray(e) ? exports.clone(e) : e | ||
}) | ||
} | ||
|
||
var exports = module.exports = function (chars, exports) { | ||
|
||
chars = chars || | ||
'!0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~' | ||
|
||
exports = exports || {} | ||
|
||
exports.randstr = randstr | ||
exports.between = between | ||
exports.strord = strord | ||
|
||
exports.lo = chars[0] | ||
exports.hi = chars[chars.length - 1] | ||
|
||
function randstr(l) { | ||
var str = '' | ||
while(l--) | ||
str += chars[ | ||
Math.floor( | ||
Math.random() * chars.length | ||
) | ||
] | ||
return str | ||
} | ||
|
||
/* | ||
SOME EXAMPLE STRINGS, IN ORDER | ||
0 | ||
00001 | ||
0001 | ||
001 | ||
001001 | ||
00101 | ||
0011 | ||
0011001 | ||
001100101 | ||
00110011 | ||
001101 | ||
00111 | ||
01 | ||
if you never make a string that ends in the lowest char, | ||
then it is always possible to make a string between two strings. | ||
this is like how decimals never end in 0. | ||
example: | ||
between('A', 'AB') | ||
... 'AA' will sort between 'A' and 'AB' but then it is impossible | ||
to make a string inbetween 'A' and 'AA'. | ||
instead, return 'AAB', then there will be space. | ||
*/ | ||
|
||
function between (a, b) { | ||
|
||
|
||
var s = '', i = 0, c | ||
|
||
var i = 0 | ||
while (true) { | ||
|
||
var _a = chars.indexOf(a[i]) | ||
var _b = chars.indexOf(b[i]) | ||
|
||
if(_a == -1) _a = 0 | ||
if(_b == -1) _b = chars.length - 1 | ||
|
||
i++ | ||
|
||
if(_a + 1 < _b) | ||
c = chars[Math.round((_a+_b)/2)] | ||
else | ||
c = chars[_a] | ||
|
||
s += c | ||
|
||
/* | ||
console.log() | ||
console.log('<<<<', a) | ||
console.log('====', s) | ||
console.log('>>>>', b) | ||
console.log(a < s && s < b) | ||
/**/ | ||
|
||
if(a < s && s < b && c != '!') | ||
break; | ||
|
||
} | ||
|
||
return s | ||
} | ||
|
||
function strord (a, b) { | ||
return ( | ||
a == b ? 0 | ||
: a < b ? -1 | ||
: 1 | ||
) | ||
} | ||
|
||
function concat(to, from) { | ||
while(from.length) | ||
to.push(from.shift()) | ||
return to | ||
} | ||
|
||
return exports | ||
} | ||
|
||
exports(null, exports) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ "name": "between" | ||
, "version": "0.0.0" | ||
, "description": "generate arbitary strings that sort between two strings" | ||
, "homepage": "http://github.com/dominictarr/between" | ||
, "repository": | ||
{ "type": "git" | ||
, "url": "https://github.com/dominictarr/between.git" } | ||
, "dependencies": {} | ||
, "devDependencies": { | ||
"assertions" : "2", | ||
"asynct" : "1.1" | ||
} | ||
, "scripts": { | ||
"test" : "asynct test/index.js" | ||
} | ||
, "author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)" } |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var u = require('../') | ||
var a = require('assertions') | ||
|
||
function _(l, b, h) { | ||
a.equal(u.between(l, h), b) | ||
} | ||
|
||
exports.between = function (t) { | ||
|
||
function assertBetween (lo, hi, depth) { | ||
var b = u.between(lo, hi) | ||
|
||
/* | ||
console.log(depth) | ||
console.log('<<<<', lo) | ||
console.log('====', b) | ||
console.log('>>>>', hi) | ||
/**/ | ||
|
||
a.greaterThan(b , lo) | ||
a.lessThan(b , hi) | ||
|
||
if(!depth) return | ||
if(~~(Math.random()*2)) { | ||
assertBetween(lo, b, depth - 1) | ||
//assertBetween(b, hi, depth - 1) | ||
}else{ | ||
assertBetween(b, hi, depth - 1) | ||
//assertBetween(lo, b, depth - 1) | ||
} | ||
} | ||
|
||
assertBetween('!', '~', 200) | ||
|
||
t.end() | ||
} | ||
/* | ||
same as above but this time, append a random string to each. | ||
(I'm gonna use this to generate concurrently ordered strings | ||
that are unlikely to collide) | ||
*/ | ||
exports.between2 = function (t) { | ||
|
||
function assertBetween (lo, hi, depth) { | ||
var b = u.between(lo, hi) + u.randstr(5) | ||
|
||
a.greaterThan(b , lo) | ||
a.lessThan(b , hi) | ||
|
||
if(!depth) return | ||
if(~~(Math.random()*2)) { | ||
assertBetween(lo, b, depth - 1) | ||
//assertBetween(b, hi, depth - 1) | ||
}else{ | ||
assertBetween(b, hi, depth - 1) | ||
//assertBetween(lo, b, depth - 1) | ||
} | ||
} | ||
|
||
assertBetween('!', '~', 200) | ||
|
||
t.end() | ||
} | ||
|