-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From Array to Uint32Array and back!
- Loading branch information
Showing
13 changed files
with
254 additions
and
78 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 |
---|---|---|
@@ -1,38 +1,39 @@ | ||
{ | ||
"name": "enofjs", | ||
"description": "Enhancing javascript", | ||
"version": "1.2.0", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"grunt": "^0.4", | ||
"load-grunt-tasks": "^0.1", | ||
"grunt-contrib-jshint": "^0.6", | ||
"grunt-contrib-watch": "^0.5", | ||
"grunt-contrib-uglify": "^0.2", | ||
"time-grunt": "^0.1", | ||
"grunt-karma": "^0.6", | ||
"karma-script-launcher": "^0.1", | ||
"karma-chrome-launcher": "^0.1", | ||
"karma-firefox-launcher": "^0.1", | ||
"karma-html2js-preprocessor": "^0.1", | ||
"karma-requirejs": "^0", | ||
"karma-jasmine": "^0.1", | ||
"karma-coffee-preprocessor": "^0.1", | ||
"karma-phantomjs-launcher": "^0.1", | ||
"karma": "^0.10", | ||
"karma-ng-html2js-preprocessor": "^0.1", | ||
"karma-coverage": "^0.1.0", | ||
"grunt-groc": "^0.4", | ||
"grunt-shell": "^0.7" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:EnoF/EnoFJS.git" | ||
} | ||
"name": "enofjs", | ||
"description": "Enhancing javascript", | ||
"version": "1.2.1", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"grunt": "^0.4", | ||
"load-grunt-tasks": "^0.1", | ||
"grunt-contrib-jshint": "^0.6", | ||
"grunt-contrib-watch": "^0.5", | ||
"grunt-contrib-uglify": "^0.2", | ||
"time-grunt": "^0.1", | ||
"grunt-karma": "^0.6", | ||
"karma-script-launcher": "^0.1", | ||
"karma-chrome-launcher": "^0.1", | ||
"karma-firefox-launcher": "^0.1", | ||
"karma-html2js-preprocessor": "^0.1", | ||
"karma-requirejs": "^0", | ||
"karma-jasmine": "^0.1", | ||
"karma-coffee-preprocessor": "^0.1", | ||
"karma-phantomjs-launcher": "^0.1", | ||
"karma": "^0.10", | ||
"karma-ng-html2js-preprocessor": "^0.1", | ||
"karma-coverage": "^0.1.0", | ||
"grunt-groc": "^0.4", | ||
"grunt-shell": "^0.7", | ||
"grunt-version": "~0.3.0" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:EnoF/EnoFJS.git" | ||
} | ||
} |
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,38 @@ | ||
// EnoFJS | ||
// Version: 1.2.1 | ||
// | ||
// Copyright (c) 2014. | ||
// | ||
// Author Andy Tang | ||
// Fork me on Github: https://github.com/EnoF/EnoFJS | ||
(function ArrayConvertersScope(Array, Uint8Array, Uint32Array) { | ||
'use strict'; | ||
|
||
// The Array buffer will expect the combined 4 bytes in reversed order. | ||
function toUint32Array() { | ||
// jshint validthis:true | ||
var self = this; | ||
var uint32Array = new Uint32Array(self.length / 4); | ||
for (var i = 0; i < self.length; i = i + 4) { | ||
uint32Array[i / 4] = (self[i + 3] << 24 | | ||
self[i + 2] << 16 | | ||
self[i + 1] << 8 | | ||
self[i]) >>> 0; | ||
} | ||
return uint32Array; | ||
} | ||
|
||
// This should actually work with other Typed arrays aswell, but I haven't tested this yet! | ||
function readUint32ArrayIn(uInt32) { | ||
// jshint validthis:true | ||
var self = this; | ||
var uInt8 = new Uint8Array(uInt32.buffer); | ||
for (var i = 0; i < uInt8.length; i = i + 1) { | ||
self[i] = uInt8[i]; | ||
} | ||
} | ||
|
||
Array.prototype.toUint32Array = toUint32Array; | ||
Array.prototype.readUint32ArrayIn = readUint32ArrayIn; | ||
|
||
}(window.Array, window.Uint8Array, window.Uint32Array)); |
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
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,109 @@ | ||
// EnoFJS | ||
// Version: 1.2.1 | ||
// | ||
// Copyright (c) 2014. | ||
// | ||
// Author Andy Tang | ||
// Fork me on Github: https://github.com/EnoF/EnoFJS | ||
(function ArrayConvertersSpecScope() { | ||
'use strict'; | ||
|
||
describe('Converting', function ArrayConvertersSpec() { | ||
|
||
describe('Normal arrays initialized as []', function NormalArrays() { | ||
var array; | ||
var convertedArray; | ||
|
||
beforeEach(function prepareArray() { | ||
array = []; | ||
|
||
array[0] = 0; | ||
array[1] = 0; | ||
array[2] = 0; | ||
array[3] = 255; | ||
|
||
array[4] = 0; | ||
array[5] = 0; | ||
array[6] = 59; | ||
array[7] = 0; | ||
|
||
array[8] = 0; | ||
array[9] = 35; | ||
array[10] = 0; | ||
array[11] = 0; | ||
|
||
array[12] = 69; | ||
array[13] = 0; | ||
array[14] = 0; | ||
array[15] = 0; | ||
|
||
array[16] = 69; | ||
array[17] = 35; | ||
array[18] = 59; | ||
array[19] = 255; | ||
|
||
convertedArray = array.toUint32Array(); | ||
}); | ||
|
||
it('should place the fourth value at the first value', function convert() { | ||
expect(convertedArray[0]).toEqual(4278190080); | ||
}); | ||
|
||
it('should place the third value at the second value', function convert() { | ||
expect(convertedArray[1]).toEqual(3866624); | ||
}); | ||
|
||
it('should place the second value at the third value', function convert() { | ||
expect(convertedArray[2]).toEqual(8960); | ||
}); | ||
|
||
it('should place the first value at the fourth value', function convert() { | ||
expect(convertedArray[3]).toEqual(69); | ||
}); | ||
|
||
it('should convert a complete byte accordingly', function complete() { | ||
expect(convertedArray[4]).toEqual(4282065733); | ||
}); | ||
}); | ||
|
||
describe('Read from Uint32Array', function Uint32ArraysToArray() { | ||
var array; | ||
var uIntArray; | ||
|
||
beforeEach(function prepareUint32Array() { | ||
array = []; | ||
uIntArray = new Uint32Array(5); | ||
uIntArray[0] = 4278190080; | ||
uIntArray[1] = 3866624; | ||
uIntArray[2] = 8960; | ||
uIntArray[3] = 69; | ||
uIntArray[4] = 4282065733; | ||
array.readUint32ArrayIn(uIntArray); | ||
}); | ||
|
||
it('should retrieve the alpha back', function alpha() { | ||
expect(array[3]).toEqual(255); | ||
}); | ||
|
||
it('should retrieve the blue value back', function blue() { | ||
expect(array[6]).toEqual(59); | ||
}); | ||
|
||
it('should retrieve the green value back', function green() { | ||
expect(array[9]).toEqual(35); | ||
}); | ||
|
||
it('should retrieve the red value back', function red() { | ||
expect(array[12]).toEqual(69); | ||
}); | ||
|
||
it('should retrieve all values back', function all() { | ||
expect(array[16]).toEqual(69); | ||
expect(array[17]).toEqual(35); | ||
expect(array[18]).toEqual(59); | ||
expect(array[19]).toEqual(255); | ||
}); | ||
}); | ||
}); | ||
|
||
}()); |
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