Skip to content

Commit

Permalink
Added Array Converters
Browse files Browse the repository at this point in the history
From Array to Uint32Array and back!
  • Loading branch information
EnoF committed May 7, 2014
1 parent e8b3e14 commit 63e8265
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"node": true,
"browser": true,
"esnext": true,
"bitwise": true,
"bitwise": false,
"camelcase": true,
"curly": true,
"eqeqeq": true,
Expand Down
27 changes: 22 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright (c) 2014.
*
* @Author Andy Tang
*/
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
'use strict';

module.exports = function (grunt) {
Expand Down Expand Up @@ -57,6 +59,21 @@ module.exports = function (grunt) {
]
}
}
},
version: {
options: {
prefix: 'Version: |\"version\": \"'
},
defaults: {
src: [
'*.js',
'bower.json',
'src/{,*/}*.js',
'test/{,*/}*.js',
'!**/lib/**',
'!**/bower_components/**'
]
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "enofjs",
"version": "1.2.0",
"version": "1.2.1",
"main": "src/enofjs",
"devDependencies": {
"jasmine": "^2.0"
Expand Down
73 changes: 37 additions & 36 deletions package.json
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"
}
}
38 changes: 38 additions & 0 deletions src/ArrayConverters.js
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));
11 changes: 6 additions & 5 deletions src/ClassFactory.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// EnoFJS v1.1.4

// Copyright (c) 2014.
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
(function ClassScope(window, undefined) {
'use strict';

Expand Down
11 changes: 6 additions & 5 deletions src/LinkedHashMap.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// EnoFJS v1.1.3

// Copyright (c) 2014.
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
(function LinkedHashMapScope(window, clazz, undefined) {
'use strict';

Expand Down
11 changes: 6 additions & 5 deletions src/whereIt.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// EnoFJS v1.1.3

// Copyright (c) 2014.
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
(function whereIt(it, LinkedHashMap) {
'use strict';

Expand Down
12 changes: 7 additions & 5 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright (c) 2014.
*
* @Author Andy Tang
*/
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
'use strict';
module.exports = function (config) {
config.set({
Expand Down
109 changes: 109 additions & 0 deletions test/spec/ArrayConvertersSpec.js
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);
});
});
});

}());
12 changes: 7 additions & 5 deletions test/spec/ClassFactorySpec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright (c) 2014.
*
* @Author Andy Tang
*/
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
(function (undefined) {
'use strict';

Expand Down
12 changes: 7 additions & 5 deletions test/spec/LinkedHashMapSpec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright (c) 2014.
*
* @Author Andy Tang
*/
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
(function LinkedHashMapSpecScope(undefined) {
'use strict';

Expand Down
12 changes: 7 additions & 5 deletions test/spec/whereItSpec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/*
* Copyright (c) 2014.
*
* @Author Andy Tang
*/
// EnoFJS
// Version: 1.2.1
//
// Copyright (c) 2014.
//
// Author Andy Tang
// Fork me on Github: https://github.com/EnoF/EnoFJS
(function whereItSpec() {
'use strict';

Expand Down

0 comments on commit 63e8265

Please sign in to comment.