diff --git a/lib/profiles/array-sort/array.sort.profile.test.js b/lib/profiles/array-sort/array.sort.profile.test.js new file mode 100644 index 0000000..ab10974 --- /dev/null +++ b/lib/profiles/array-sort/array.sort.profile.test.js @@ -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'); + } + }); + }); + }); +}); diff --git a/lib/profiles/array-sort/index.js b/lib/profiles/array-sort/index.js new file mode 100644 index 0000000..5d23287 --- /dev/null +++ b/lib/profiles/array-sort/index.js @@ -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', +};