Skip to content

Commit

Permalink
Merge tag '1.0.14' into develop
Browse files Browse the repository at this point in the history
no message
  • Loading branch information
bshaoatenvisiodotcom committed Jan 13, 2020
2 parents b398cdd + 37bf8c9 commit 0dc7f99
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sort-by-chapter",
"version": "1.0.13",
"version": "1.0.14",
"main": "./lib/index.js",
"license": "MIT",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ module.exports = (arr, option) => {

switch (typeof option) {
case 'object':
if (option.attribute && arr.every((obj) => Object.prototype.hasOwnProperty.call(obj, option.attribute))) {
if (option.attribute && arr.every((obj) => Object.prototype.hasOwnProperty.call(obj, option.attribute) && typeof obj[option.attribute] === 'string')) {
const { attribute: attr } = option;

return JSON.parse(JSON.stringify(arr)).sort((a, b) => a[attr].match(/(\d+\.*)+/)[0].localeCompare(b[attr].match(/(\d+\.*)+/)[0], undefined, { numeric: true }));
}

return arr;
case 'string':
if (arr.every((obj) => Object.prototype.hasOwnProperty.call(obj, option))) {
if (arr.every((obj) => Object.prototype.hasOwnProperty.call(obj, option) && obj[option])) {
return JSON.parse(JSON.stringify(arr)).sort((a, b) => a[option].match(/(\d+\.*)+/)[0].localeCompare(b[option].match(/(\d+\.*)+/)[0], undefined, { numeric: true }));
}

Expand Down
24 changes: 24 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ const objArr = [
{ title: 'Activity 1.1.3' },
];

const objArr2 = [
{ title: 'Goal 1' },
{ title: 'Goal 2' },
{ title: 'Goal 3' },
{ title: undefined },
{ title: 'Strategy 1.2' },
{ title: 'Strategy 1.3' },
{ title: 'Activity 1.1.1' },
{ title: 'Activity 1.1.2' },
{ title: 'Activity 1.1.3' },
];

describe('Array', () => {
const [first, , , , , , , , last] = sortByChapter(arr);

Expand Down Expand Up @@ -77,3 +89,15 @@ describe('Array of Object with Invalid Attribute Option', () => {
it('First should be "Goal 1"', () => expect(first.title).to.equal('Goal 1'));
it('Last should be "Activity 1.1.3"', () => expect(last.title).to.equal('Activity 1.1.3'));
});

describe('Array of Object with String Option and Invalid Attribute', () => {
const [first, , , , , , , , last] = sortByChapter(objArr2, 'title');
it('First should be "Goal 1"', () => expect(first.title).to.equal('Goal 1'));
it('Last should be "Activity 1.1.3"', () => expect(last.title).to.equal('Activity 1.1.3'));
});

describe('Array of Object with Object Option and Invalid Attribute', () => {
const [first, , , , , , , , last] = sortByChapter(objArr2, { attribute: 'title' });
it('First should be "Goal 1"', () => expect(first.title).to.equal('Goal 1'));
it('Last should be "Activity 1.1.3"', () => expect(last.title).to.equal('Activity 1.1.3'));
});

0 comments on commit 0dc7f99

Please sign in to comment.