Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed May 15, 2012
0 parents commit 7ab1a02
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
node_modules/*
npm_debug.log
120 changes: 120 additions & 0 deletions index.js
@@ -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)
16 changes: 16 additions & 0 deletions package.json
@@ -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 added readme.markdown
Empty file.
64 changes: 64 additions & 0 deletions test/index.js
@@ -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()
}

0 comments on commit 7ab1a02

Please sign in to comment.