forked from haensl/js-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Profile request: sort haensl#123 : partial implementation | sort int,…
…float,string
- Loading branch information
1 parent
5c70bbb
commit ce07167
Showing
2 changed files
with
73 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,34 @@ | ||
const expect = require('chai').expect; | ||
const sorts = require('./'); | ||
|
||
describe('Array sort', () => { | ||
let result; | ||
let data; | ||
|
||
sorts.functions.forEach((fn) => { | ||
describe(fn.description, () => { | ||
|
||
it(`sorts the array using ${fn.description}`, () => { | ||
switch (fn) { | ||
case sorts.functions[0]: | ||
data = [[4, 3, 5, 2, 1, 0]]; | ||
result = fn.f(data); | ||
expect(result).to.eql([0, 1, 2, 3, 4, 5]); // Expected result for integers | ||
break; | ||
case sorts.functions[1]: | ||
data = [[4.0, 3.0, 5.0, 2.0, 1.0, 0]]; | ||
result = fn.f(data); | ||
expect(result).to.eql([0, 1.0, 2.0, 3.0, 4.0, 5.0]); // Expected result for floats | ||
break; | ||
case sorts.functions[2]: | ||
data = [['4', '3', '5', '2', '1', '0']]; | ||
result = fn.f(data); | ||
expect(result).to.eql(['0', '1', '2', '3', '4', '5']); // Expected result for strings | ||
break; | ||
default: | ||
throw new Error('Invalid function'); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
const unique = require('../../support/array').unique; | ||
|
||
const sortArrayInteger = { | ||
description: 'Array\'s sort() method on Integers', | ||
keywords: ['array', 'sort', 'method'].sort(), | ||
codeSample: 'a.sort()', | ||
f: (d) => { return d[0].sort((a, b) => a - b); } | ||
}; | ||
|
||
const sortArrayFloat = { | ||
description: 'Array\'s sort() method on Floats', | ||
keywords: ['array', 'sort', 'method'].sort(), | ||
codeSample: 'a.sort()', | ||
f: (d) => { return d[0].sort((a, b) => a - b); } | ||
}; | ||
|
||
const sortArrayString = { | ||
description: 'Array\'s sort() method on Strings', | ||
keywords: ['array', 'sort', 'method'].sort(), | ||
codeSample: 'a.sort()', | ||
f: (d) => { return d[0].sort(); } | ||
}; | ||
|
||
const functions = [sortArrayInteger, sortArrayFloat, sortArrayString]; | ||
|
||
module.exports = { | ||
name: 'array sort', | ||
description: { | ||
long: 'Array sort on different data types.', | ||
short: 'Array sort variations.', | ||
}, | ||
keywords: unique( | ||
functions | ||
.map((fn) => fn.keywords) | ||
.reduce((keywords, fnKeywords) => [...keywords, ...fnKeywords]) | ||
).sort(), | ||
functions, | ||
testDataType: 'arrays', | ||
}; |