Skip to content

Commit

Permalink
Profile request: sort haensl#123 : partial implementation | sort int,…
Browse files Browse the repository at this point in the history
…float,string
  • Loading branch information
MadhuNimmo committed Dec 13, 2023
1 parent 5c70bbb commit ce07167
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/profiles/array-sort/array.sort.profile.test.js
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');
}
});
});
});
});
39 changes: 39 additions & 0 deletions lib/profiles/array-sort/index.js
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',
};

0 comments on commit ce07167

Please sign in to comment.