feat: add simple challengeFile sorter
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
b0f18cacc7
commit
f82886148c
@ -0,0 +1,38 @@
|
||||
export const challengeFiles = {
|
||||
indexcss: {
|
||||
contents: 'some css',
|
||||
error: null,
|
||||
ext: 'css',
|
||||
head: '',
|
||||
history: ['index.css'],
|
||||
key: 'indexcss',
|
||||
name: 'index',
|
||||
path: 'index.css',
|
||||
seed: 'some css',
|
||||
tail: ''
|
||||
},
|
||||
indexhtml: {
|
||||
contents: 'some html',
|
||||
error: null,
|
||||
ext: 'html',
|
||||
head: '',
|
||||
history: ['index.html'],
|
||||
key: 'indexhtml',
|
||||
name: 'index',
|
||||
path: 'index.html',
|
||||
seed: 'some html',
|
||||
tail: ''
|
||||
},
|
||||
indexjs: {
|
||||
contents: 'some js',
|
||||
error: null,
|
||||
ext: 'js',
|
||||
head: '',
|
||||
history: ['index.js'],
|
||||
key: 'indexjs',
|
||||
name: 'index',
|
||||
path: 'index.js',
|
||||
seed: 'some js',
|
||||
tail: ''
|
||||
}
|
||||
};
|
13
client/src/templates/Challenges/utils/sort-files.js
Normal file
13
client/src/templates/Challenges/utils/sort-files.js
Normal file
@ -0,0 +1,13 @@
|
||||
export function sortFiles(challengeFiles) {
|
||||
const xs = Object.values(challengeFiles);
|
||||
// TODO: refactor this to use an ext array ['html', 'js', 'css'] and loop over
|
||||
// that.
|
||||
xs.sort((a, b) => {
|
||||
if (a.ext === 'html') return -1;
|
||||
if (b.ext === 'html') return 1;
|
||||
if (a.ext === 'js') return -1;
|
||||
if (b.ext === 'js') return 1;
|
||||
return 0;
|
||||
});
|
||||
return xs;
|
||||
}
|
26
client/src/templates/Challenges/utils/sort-files.test.js
Normal file
26
client/src/templates/Challenges/utils/sort-files.test.js
Normal file
@ -0,0 +1,26 @@
|
||||
/* global expect */
|
||||
|
||||
const { sortFiles } = require('./sort-files');
|
||||
const { challengeFiles } = require('./__fixtures__/challenges');
|
||||
|
||||
describe('sort-files', () => {
|
||||
describe('sortFiles', () => {
|
||||
it('should return an array', () => {
|
||||
const sorted = sortFiles(challengeFiles);
|
||||
expect(Array.isArray(sorted)).toBe(true);
|
||||
});
|
||||
it('should not modify the challenges', () => {
|
||||
const sorted = sortFiles(challengeFiles);
|
||||
const expected = Object.values(challengeFiles);
|
||||
expect(sorted).toEqual(expect.arrayContaining(expected));
|
||||
expect(sorted.length).toEqual(expected.length);
|
||||
});
|
||||
|
||||
it('should sort the objects into html, js, css order', () => {
|
||||
const sorted = sortFiles(challengeFiles);
|
||||
const sortedKeys = sorted.map(({ key }) => key);
|
||||
const expected = ['indexhtml', 'indexjs', 'indexcss'];
|
||||
expect(sortedKeys).toStrictEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user