-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from GrimoireGL/feat/number-array
feat: add number array converter
- Loading branch information
Showing
3 changed files
with
42 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Attribute from "grimoirejs/ref/Node/Attribute"; | ||
function NumberArrayConverter(this: Attribute, val: any): any { | ||
if(typeof val === "string"){ | ||
return new Float32Array(val.split(",").map(f=>parseFloat(f))); | ||
}else if(Array.isArray(val)){ | ||
return new Float32Array(val); | ||
}else if(val instanceof Float32Array){ | ||
return val; | ||
} | ||
} | ||
|
||
export default NumberArrayConverter; |
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,28 @@ | ||
import test from 'ava'; | ||
import NumberArrayConverter from '../lib-es5/Converters/NumberArrayConverter'; | ||
|
||
test('parse from string', (t) => { | ||
const arr = NumberArrayConverter("1,2,3,4,5"); | ||
const correct = [1,2,3,4,5]; | ||
t.true(arr instanceof Float32Array); | ||
t.true(correct.filter((v,i)=>arr[i] !== v).length === 0); | ||
t.true(arr.length === correct.length); | ||
}); | ||
|
||
|
||
test('parse from raw array', (t) => { | ||
const arr = NumberArrayConverter([1,2,3,4,5]); | ||
const correct = [1,2,3,4,5]; | ||
t.true(arr instanceof Float32Array); | ||
t.true(correct.filter((v,i)=>arr[i] !== v).length === 0); | ||
t.true(arr.length === correct.length); | ||
}); | ||
|
||
|
||
test('parse from Float32Array', (t) => { | ||
const arr = NumberArrayConverter(new Float32Array([1,2,3,4,5])); | ||
const correct = [1,2,3,4,5]; | ||
t.true(arr instanceof Float32Array); | ||
t.true(correct.filter((v,i)=>arr[i] !== v).length === 0); | ||
t.true(arr.length === correct.length); | ||
}); |