Last active
October 23, 2019 09:42
-
-
Save nidin/cfac024e3cf39ed4c028705f6c41574d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
describe("When passing an unknown element type", () => { | |
it("Should throw error", () => { | |
const type = "unknown-type"; | |
const array = [1]; | |
expect(() => { | |
$sort([array, 0, <any>type]); | |
}).toThrowError(`Unknown array element type:${type}`); | |
}); | |
}); | |
describe("When passing an unsorted Uint32Array", () => { | |
it("Should return sorted Uint32Array", () => { | |
const array = [34, 456, 8, 30, 246, 32, 26, 31, 25]; | |
const result = $sort([array, 0, "u32"]); | |
const sortedArray = [8, 25, 26, 30, 31, 32, 34, 246, 456]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted Int32Array", () => { | |
it("Should return sorted Int32Array", () => { | |
const array = [34, 456, -8, 30, 246, 32, 26, 31, 25]; | |
const result = $sort([array, 0, "i32"]); | |
const sortedArray = [-8, 25, 26, 30, 31, 32, 34, 246, 456]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted Float32Array", () => { | |
it("Should return sorted Float32Array", () => { | |
const array = [ | |
34.56, | |
456.46, | |
8.164, | |
30.685, | |
246.165, | |
32.614, | |
26.6154, | |
31.124, | |
25.1656 | |
]; | |
const result = $sort([array, 0, "f32"]); | |
const sortedArray = [ | |
8.164, | |
25.1656, | |
26.6154, | |
30.685, | |
31.124, | |
32.614, | |
34.56, | |
246.165, | |
456.46 | |
]; | |
expect(result.toString()).toEqual(sortedArray.toString()); | |
}); | |
}); | |
describe("When passing an unsorted float 32 string array", () => { | |
it("Should return sorted array using Float32Array.sort", () => { | |
const array = [ | |
"34.56", | |
"456.46", | |
"8.164", | |
"30.685", | |
"246.165", | |
"32.614", | |
"26.6154", | |
"31.124", | |
"25.1656" | |
]; | |
const result = $sort([array, 0, "f32"]); | |
const sortedArray = [ | |
"8.164", | |
"25.1656", | |
"26.6154", | |
"30.685", | |
"31.124", | |
"32.614", | |
"34.56", | |
"246.165", | |
"456.46" | |
]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted Float64Array", () => { | |
it("Should return sorted Float64Array", () => { | |
const array = [ | |
34.56, | |
456.46, | |
8.164, | |
30.685, | |
246.165, | |
32.614, | |
26.6154, | |
31.124, | |
25.1656 | |
]; | |
const result = $sort([array, 0, "f64"]); | |
const sortedArray = [ | |
8.164, | |
25.1656, | |
26.6154, | |
30.685, | |
31.124, | |
32.614, | |
34.56, | |
246.165, | |
456.46 | |
]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted float 64 string array", () => { | |
it("Should return sorted array using Float64Array.sort", () => { | |
const array = [ | |
"34.56", | |
"456.46", | |
"8.164", | |
"30.685", | |
"246.165", | |
"32.614", | |
"26.6154", | |
"31.124", | |
"25.1656" | |
]; | |
const result = $sort([array, 0, "f64"]); | |
const sortedArray = [ | |
"8.164", | |
"25.1656", | |
"26.6154", | |
"30.685", | |
"31.124", | |
"32.614", | |
"34.56", | |
"246.165", | |
"456.46" | |
]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
// | |
describe("When passing an unsorted Uint32Array", () => { | |
it("Should return sorted Uint32Array", () => { | |
const code = { | |
$sort: ["$array", 0, "u32"] | |
}; | |
const context = { | |
array: [34, 456, 8, 30, 246, 32, 26, 31, 25] | |
}; | |
const result = evaluateCode(code, context); | |
const sortedArray = [8, 25, 26, 30, 31, 32, 34, 246, 456]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted Int32Array", () => { | |
it("Should return sorted Int32Array", () => { | |
const code = { | |
$sort: ["$array", 0, "i32"] | |
}; | |
const context = { | |
array: [34, 456, -8, 30, 246, 32, 26, 31, 25] | |
}; | |
const result = evaluateCode(code, context); | |
const sortedArray = [-8, 25, 26, 30, 31, 32, 34, 246, 456]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted Float32Array", () => { | |
it("Should return sorted Float32Array", () => { | |
const code = { | |
$sort: ["$array", 0, "f32"] | |
}; | |
const context = { | |
array: [ | |
34.56, | |
456.46, | |
8.164, | |
30.685, | |
246.165, | |
32.614, | |
26.6154, | |
31.124, | |
25.1656 | |
] | |
}; | |
const result = evaluateCode(code, context); | |
const sortedArray = [ | |
8.164, | |
25.1656, | |
26.6154, | |
30.685, | |
31.124, | |
32.614, | |
34.56, | |
246.165, | |
456.46 | |
]; | |
expect(result.toString()).toEqual(sortedArray.toString()); | |
}); | |
}); | |
describe("When passing an unsorted float 32 string array", () => { | |
it("Should return sorted array using Float32Array.sort", () => { | |
const code = { | |
$sort: ["$array", 0, "f32"] | |
}; | |
const context = { | |
array: [ | |
"34.56", | |
"456.46", | |
"8.164", | |
"30.685", | |
"246.165", | |
"32.614", | |
"26.6154", | |
"31.124", | |
"25.1656" | |
] | |
}; | |
const result = evaluateCode(code, context); | |
const sortedArray = [ | |
"8.164", | |
"25.1656", | |
"26.6154", | |
"30.685", | |
"31.124", | |
"32.614", | |
"34.56", | |
"246.165", | |
"456.46" | |
]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted Float64Array", () => { | |
it("Should return sorted Float64Array", () => { | |
const code = { | |
$sort: ["$array", 0, "f64"] | |
}; | |
const context = { | |
array: [ | |
34.56, | |
456.46, | |
8.164, | |
30.685, | |
246.165, | |
32.614, | |
26.6154, | |
31.124, | |
25.1656 | |
] | |
}; | |
const result = evaluateCode(code, context); | |
const sortedArray = [ | |
8.164, | |
25.1656, | |
26.6154, | |
30.685, | |
31.124, | |
32.614, | |
34.56, | |
246.165, | |
456.46 | |
]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unsorted float 64 string array", () => { | |
it("Should return sorted array using Float64Array.sort", () => { | |
const code = { | |
$sort: ["$array", 0, "f64"] | |
}; | |
const context = { | |
array: [ | |
"34.56", | |
"456.46", | |
"8.164", | |
"30.685", | |
"246.165", | |
"32.614", | |
"26.6154", | |
"31.124", | |
"25.1656" | |
] | |
}; | |
const result = evaluateCode(code, context); | |
const sortedArray = [ | |
"8.164", | |
"25.1656", | |
"26.6154", | |
"30.685", | |
"31.124", | |
"32.614", | |
"34.56", | |
"246.165", | |
"456.46" | |
]; | |
expect(result).toEqual(sortedArray); | |
}); | |
}); | |
describe("When passing an unknown element type", () => { | |
it("Should throw error", () => { | |
const type = "unknown-type"; | |
const code = { | |
$sort: ["$array", 0, type] | |
}; | |
const context = { | |
array: [1] | |
}; | |
expect(() => { | |
evaluateCode(code, context); | |
}).toThrowError(`Unknown array element type:${type}`); | |
}); | |
}); |
This file contains hidden or 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
type ArrayElementTypeValue = "any" | "i32" | "u32" | "f32" | "f64"; | |
enum ArrayElementType { | |
ANY = "any", | |
I32 = "i32", | |
U32 = "u32", | |
F32 = "f32", | |
F64 = "f64" | |
} | |
type TypedArray = Int32Array | Uint32Array | Float32Array | Float64Array; | |
function indexTypedArray(typedArray: TypedArray) { | |
const indices: { element: number; index: number }[] = []; | |
for (let i = 0; i < typedArray.length; i += 1) { | |
const element = typedArray[i]; | |
indices.push({ | |
element, | |
index: i | |
}); | |
} | |
return { | |
typedArray, | |
indices | |
}; | |
} | |
function mapTypedArray(array: any[], type: ArrayElementTypeValue) { | |
switch (type) { | |
case ArrayElementType.I32: | |
return indexTypedArray(new Int32Array(array)); | |
case ArrayElementType.U32: | |
return indexTypedArray(new Uint32Array(array)); | |
case ArrayElementType.F32: | |
return indexTypedArray(new Float32Array(array)); | |
case ArrayElementType.F64: | |
return indexTypedArray(new Float64Array(array)); | |
default: | |
throw new Error(`Unknown array element type:${type}`); | |
} | |
} | |
function sortWithTypedArray( | |
array: any[], | |
elementType: ArrayElementTypeValue, | |
sortFunction: ((a: number, b: number) => number) | undefined | |
) { | |
const { indices, typedArray } = mapTypedArray(array, elementType); | |
const sortedTypedArray = typedArray.sort(sortFunction); | |
const sortedArray = []; | |
const foundIndices: number[] = []; | |
for (let i = 0; i < sortedTypedArray.length; i += 1) { | |
const element = sortedTypedArray[i]; | |
const found = indices.find( | |
item => item.element === element && !foundIndices.includes(item.index) | |
); | |
if (found) { | |
foundIndices.push(found.index); | |
sortedArray.push(array[found.index]); | |
} | |
} | |
return sortedArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment