S
Scott Sauyet
I'm hoping some people here might be willing to critique the API I'm
designing for a compare-function generator.
Several times lately I've had to do some sorting on formatted
strings. In each case, it's been fairly easy to do a one-off sorter
for the given format, but I'm tired of writing them. So I started to
build a generic one, but it had an interface that seemed too clunky,
and I tried again. This one seems to me to be clean and easy to use,
but the syntax looks strange for a JS API, and I wonder if I'm just
trying to be clever.
Here are a few examples. Let's say we have an array that looks like
this:
["12-A", "11-B", "1-a", "2-C", "2-B", "2-A",
"1-A", "4-B", "13-C", "12-C", "3-B", "3-A"];
And I want them sorted so that "11-A" comes after "2-A", not before.
I generate a compare function like this:
var sorter1 = sorter.numeric().fixed("-").alpha();
If I want the numbers descending, but the Strings ascending, I would
use this:
var sorter2 = sorter.numeric({descending: true})
.fixed("-")
.alpha();
And if I wanted to convert this:
["12a:1", "12A:11", "12a:2", "a2C:123", "A2C:14"]
into this:
["12a:1", "12a:2", "12A:11", "A2C:14", "a2C:123"]
I would use something like this:
var sorter3 = sorter.word({caseSenstive: true})
.fixed(":")
.numeric();
The basic API is that I expose sorter, which has four functions (so
far): "numeric", "fixed", "alpha", and "word". The function that is
returned also responds to the same functions, so that I build up my
overall sorter one section at a time.
Obviously, I could have had an API that worked like this:
var sorter3 = sorter([
{type: "word", caseSensitive: true},
{type: "fixed", value: ":"},
{type: "numeric"}
]):
But that seemed a little too clunky. Does what I've done seem
reasonable to cljs regulars?
I do have an implementation if anyone wants to look at it [1], but
it's a very rough first pass. I'm really only concerned at the moment
about whether the API seems intuitive.
Thanks for any feedback you have to offer,
-- Scott
____________________
[1] There is a test page at
http://scott.sauyet.com/Javascript/Test/Sorter/
with the actual JS at
http://scott.sauyet.com/Javascript/Test/Sorter/sorter.js
designing for a compare-function generator.
Several times lately I've had to do some sorting on formatted
strings. In each case, it's been fairly easy to do a one-off sorter
for the given format, but I'm tired of writing them. So I started to
build a generic one, but it had an interface that seemed too clunky,
and I tried again. This one seems to me to be clean and easy to use,
but the syntax looks strange for a JS API, and I wonder if I'm just
trying to be clever.
Here are a few examples. Let's say we have an array that looks like
this:
["12-A", "11-B", "1-a", "2-C", "2-B", "2-A",
"1-A", "4-B", "13-C", "12-C", "3-B", "3-A"];
And I want them sorted so that "11-A" comes after "2-A", not before.
I generate a compare function like this:
var sorter1 = sorter.numeric().fixed("-").alpha();
If I want the numbers descending, but the Strings ascending, I would
use this:
var sorter2 = sorter.numeric({descending: true})
.fixed("-")
.alpha();
And if I wanted to convert this:
["12a:1", "12A:11", "12a:2", "a2C:123", "A2C:14"]
into this:
["12a:1", "12a:2", "12A:11", "A2C:14", "a2C:123"]
I would use something like this:
var sorter3 = sorter.word({caseSenstive: true})
.fixed(":")
.numeric();
The basic API is that I expose sorter, which has four functions (so
far): "numeric", "fixed", "alpha", and "word". The function that is
returned also responds to the same functions, so that I build up my
overall sorter one section at a time.
Obviously, I could have had an API that worked like this:
var sorter3 = sorter([
{type: "word", caseSensitive: true},
{type: "fixed", value: ":"},
{type: "numeric"}
]):
But that seemed a little too clunky. Does what I've done seem
reasonable to cljs regulars?
I do have an implementation if anyone wants to look at it [1], but
it's a very rough first pass. I'm really only concerned at the moment
about whether the API seems intuitive.
Thanks for any feedback you have to offer,
-- Scott
____________________
[1] There is a test page at
http://scott.sauyet.com/Javascript/Test/Sorter/
with the actual JS at
http://scott.sauyet.com/Javascript/Test/Sorter/sorter.js