-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into padawannn/histogram-fix
- Loading branch information
Showing
11 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { applySorting } from '../src/utils/sorting'; | ||
|
||
const features = [ | ||
{ column1: 'C', column2: 3 }, | ||
{ column1: 'A', column2: 1 }, | ||
{ column1: 'B', column2: 2 }, | ||
{ column1: 'D', column2: 4 }, | ||
{ column1: 'D', column2: 5 } | ||
]; | ||
|
||
const commonSortedFeatures = [ | ||
{ column1: 'A', column2: 1 }, | ||
{ column1: 'B', column2: 2 }, | ||
{ column1: 'C', column2: 3 }, | ||
{ column1: 'D', column2: 4 }, | ||
{ column1: 'D', column2: 5 } | ||
]; | ||
|
||
const commonSortedFeatures2 = [ | ||
{ column1: 'D', column2: 4 }, | ||
{ column1: 'D', column2: 5 }, | ||
{ column1: 'C', column2: 3 }, | ||
{ column1: 'B', column2: 2 }, | ||
{ column1: 'A', column2: 1 } | ||
]; | ||
|
||
describe('Sorting', () => { | ||
test('should correctly throw error when sortOptions are invalid', () => { | ||
expect(() => applySorting(features, { sortBy: 12345 })).toThrowError(Error); | ||
}); | ||
|
||
describe('should correctly understand sortOptions', () => { | ||
test('if undefined', () => { | ||
expect(applySorting(features)).toEqual(features); | ||
}); | ||
|
||
test('if sortBy is string', () => { | ||
expect(applySorting(features, { sortBy: 'column1' })).toEqual(commonSortedFeatures); | ||
}); | ||
|
||
test('if sortBy uses 2 columns', () => { | ||
expect(applySorting(features, { sortBy: ['column1', 'column2'] })).toEqual( | ||
commonSortedFeatures | ||
); | ||
}); | ||
|
||
test('if sortBy is array of arrays', () => { | ||
expect(applySorting(features, { sortBy: [['column1'], ['column2']] })).toEqual( | ||
commonSortedFeatures | ||
); | ||
}); | ||
}); | ||
describe('should correctly sort', () => { | ||
test('if sortByDirection is used', () => { | ||
expect( | ||
applySorting(features, { sortBy: 'column1', sortByDirection: 'desc' }) | ||
).toEqual(commonSortedFeatures2); | ||
}); | ||
|
||
test('if sort direction is applied inside sortBy', () => { | ||
expect(applySorting(features, { sortBy: [['column1', 'desc']] })).toEqual( | ||
commonSortedFeatures2 | ||
); | ||
|
||
expect( | ||
applySorting(features, { sortBy: [['column1']], sortByDirection: 'desc' }) | ||
).toEqual(commonSortedFeatures2); | ||
}); | ||
|
||
test('if sort direction is applied inside sortBy and sortByDirection is also used, sortBy has priority', () => { | ||
expect( | ||
applySorting(features, { sortBy: [['column1', 'desc']], sortByDirection: 'asc' }) | ||
).toEqual(commonSortedFeatures2); | ||
|
||
expect( | ||
applySorting(features, { | ||
sortBy: [['column1', { direction: 'desc' }]], | ||
sortByDirection: 'asc' | ||
}) | ||
).toEqual(commonSortedFeatures2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { firstBy } from 'thenby'; | ||
|
||
/** | ||
* Apply sort structure to a collection of features | ||
* @param {array} features | ||
* @param {object} [sortOptions] | ||
* @param {string | string[] | object[]} [sortOptions.sortBy] - One or more columns to sort by | ||
* @param {string} [sortOptions.sortByDirection] - Direction by the columns will be sorted | ||
*/ | ||
export function applySorting(features, { sortBy, sortByDirection = 'asc' } = {}) { | ||
// If sortBy is undefined, pass all features | ||
if (sortBy === undefined) { | ||
return features; | ||
} | ||
|
||
// sortOptions exists, but are bad formatted | ||
const isValidSortBy = | ||
(Array.isArray(sortBy) && sortBy.length) || // sortBy can be an array of columns | ||
typeof sortBy === 'string'; // or just one column | ||
|
||
if (!isValidSortBy) { | ||
throw new Error('Sorting options are bad formatted'); | ||
} | ||
|
||
const sortFn = createSortFn({ | ||
sortBy, | ||
sortByDirection | ||
}); | ||
|
||
return features.sort(sortFn); | ||
} | ||
|
||
// Aux | ||
function createSortFn({ sortBy, sortByDirection }) { | ||
const [firstSortOption, ...othersSortOptions] = normalizeSortByOptions({ | ||
sortBy, | ||
sortByDirection | ||
}); | ||
|
||
let sortFn = firstBy(...firstSortOption); | ||
for (let sortOptions of othersSortOptions) { | ||
sortFn = sortFn.thenBy(...sortOptions); | ||
} | ||
|
||
return sortFn; | ||
} | ||
|
||
function normalizeSortByOptions({ sortBy, sortByDirection }) { | ||
if (!Array.isArray(sortBy)) { | ||
sortBy = [sortBy]; | ||
} | ||
|
||
return sortBy.map((sortByEl) => { | ||
// sortByEl is 'column' | ||
if (typeof sortByEl === 'string') { | ||
return [sortByEl, sortByDirection]; | ||
} | ||
|
||
if (Array.isArray(sortByEl)) { | ||
// sortBy is ['column'] | ||
if (sortByEl[1] === undefined) { | ||
return [sortByEl, sortByDirection]; | ||
} | ||
|
||
// sortBy is ['column', { ... }] | ||
if (typeof sortByEl[1] === 'object') { | ||
return [sortByEl[0], { direction: sortByDirection, ...sortByEl[1] }]; | ||
} | ||
} | ||
return sortByEl; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Methods } from './workerMethods'; | ||
import { ViewportFeaturesBinary } from '@carto/react-core'; | ||
|
||
export function executeTask(source: string, method: Methods, params: ViewportFeaturesBinary): Promise<any>; | ||
export function executeTask(source: string, method: Methods, params?: ViewportFeaturesBinary): Promise<any>; | ||
|
||
export function removeWorker(source: string): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters