Skip to content

Commit

Permalink
is sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
sconover committed Feb 3, 2011
1 parent be2cb1c commit 58dd2df
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/collection_functions.js
Expand Up @@ -18,6 +18,7 @@ CollectionFunctions = (function(){
append:function(array, item){ array.push(item) },
isCollection:function(thing){ return typeof thing.length != "undefined" },
size:function(array){ return array.length },
// sort:function(array){ [].concat(array).sort() },
concat:function(){
var firstArray = arguments[0]
var otherArrays = []
Expand All @@ -36,7 +37,7 @@ CollectionFunctions = (function(){
}

var featureNames = ["iterator", "nothing", "equals", "newCollection",
"append", "isCollection", "size", "concat"]
"append", "isCollection", "size", "concat", "comparator"]
for (var i=0; i<featureNames.length; i++) {
var featureName = featureNames[i]
var halt = featureRequirementBug
Expand Down Expand Up @@ -149,6 +150,20 @@ CollectionFunctions = (function(){
}
if (functionsForExport.map) functionsForExport.pluck = pluck

function isSorted(collection) {
var sorted = true
var previousItem = null
each(collection, function(item, i){
if (i>=1 && features.comparator(previousItem, item) < 0) {
sorted = false
return breaker
}
previousItem = item
})
return sorted
}
if (functionsForExport.each && feature("comparator")) functionsForExport.isSorted = isSorted

function indexOf(collection, findMe) {
var index = features.nothing()
detect(collection, function(item, i){
Expand Down
59 changes: 59 additions & 0 deletions spec/is_sorted_spec.js
@@ -0,0 +1,59 @@
require("./spec_helper.js");

describe("is sorted", function() {

var fArr = CollectionFunctions.Array.functions
var numberComparator = function(a,b){
if (a==b) {
return 0
} else if (a<b) {
return 1
} else {
return -1
}
}

it("determines whether a collection is sorted or not", function(){
var fMin = minimalArrayCF().appendFeatures({comparator:numberComparator}).functions

expect(fMin.isSorted([5,6])).toEqual(true)
expect(fMin.isSorted([5,6,7,8])).toEqual(true)
expect(fMin.isSorted([5,6,7,7,8])).toEqual(true)

expect(fMin.isSorted([2,3,1])).toEqual(false)
expect(fMin.isSorted([])).toEqual(true)
expect(fMin.isSorted([1])).toEqual(true)
})

describe("feature requirements", function(){

it("requires iterator and comparator", function(){
expect(CollectionFunctions({}).functions.
isSorted).toBeUndefined()

expect(CollectionFunctions({iterator:fArr.iterator}).functions.
isSorted).toBeUndefined()

expect(CollectionFunctions({iterator:fArr.iterator, comparator:numberComparator}).functions.
isSorted).toBeDefined()
})

})

describe("cost", function() {

it("cost is the number of elements in the collection, is the collection is sorted", function(){
var fMin = minimalArrayCF().appendFeatures({comparator:numberComparator}).functions
fMin.isSorted([5,6,7,8])
expect(fMin.lastCost()).toEqual(4)
})

it("cost is the number of elements to the first unsorted element, if unsorted", function(){
var fMin = minimalArrayCF().appendFeatures({comparator:numberComparator}).functions
fMin.isSorted([5,7,6,8])
expect(fMin.lastCost()).toEqual(3)
})

})
})

0 comments on commit 58dd2df

Please sign in to comment.