Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile request: sort #123 : partial implementation | sort int,float,… #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
};