Skip to content

Commit

Permalink
Remove ES6 syntax from phonegap-nfc.js
Browse files Browse the repository at this point in the history
Fixes #325
  • Loading branch information
don committed Jun 12, 2018
1 parent a1d7b3b commit e6517e1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -1108,7 +1108,7 @@ Create a new project
cordova platform add android
cordova plugin add ../phonegap-nfc
cordova plugin add ../phonegap-nfc/tests
cordova plugin add http://git-wip-us.apache.org/repos/asf/cordova-plugin-test-framework.git
cordova plugin add https://github.com/apache/cordova-plugin-test-framework.git

Change the start page in `config.xml`

Expand Down
14 changes: 14 additions & 0 deletions tests/package.json
@@ -0,0 +1,14 @@
{
"name": "com.chariotsolutions.nfc.plugin.tests",
"version": "0.0.1-dev",
"description": "Tests for phonegap-nfc",
"cordova": {
"id": "phonegap-nfc-tests",
"platforms": []
},
"keywords": [
"ecosystem:cordova"
],
"author": "Don Coleman <don.coleman@gmail.com>",
"license": "MIT"
}
2 changes: 1 addition & 1 deletion tests/plugin.xml
@@ -1,7 +1,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.chariotsolutions.nfc.plugin.tests"
id="phonegap-nfc-tests"
version="0.0.1-dev">
<name>PhoneGap NFC Plugin Tests</name>
<license>Apache 2.0</license>
Expand Down
63 changes: 62 additions & 1 deletion tests/tests.js
Expand Up @@ -9,6 +9,16 @@ exports.defineAutoTests = function () {
expect(typeof nfc.addNdefListener).toBeDefined();
expect(typeof nfc.addNdefListener).toBe("function");
});

it("should contain a readerMode function", function () {
expect(typeof nfc.readerMode).toBeDefined();
expect(typeof nfc.readerMode).toBe("function");
});

it("should contain a transceive function", function () {
expect(typeof nfc.transceive).toBeDefined();
expect(typeof nfc.transceive).toBe("function");
});
});

describe('UTF-8 Encoding and Decoding', function() {
Expand Down Expand Up @@ -65,8 +75,59 @@ exports.defineAutoTests = function () {
expect(roundTrip).toEqual(url);

});

});


describe('Translate ArrayBuffer and HEX Strings', function() {
it('should convert array buffer to hex string', function() {
var source = new Uint8Array([0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF]).buffer;
var hexString = util.arrayBufferToHexString(source);
expect(hexString).toEqual('000102fdfeff');
});

it('should convert hex string to array buffer', function() {

var hexString = '000102FDFEFF'
var buffer = util.hexStringToArrayBuffer(hexString);
expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF]));

// leading 0
var buffer = util.hexStringToArrayBuffer('0x68656c6c6f2c20776f726c64');
expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));

// space delimiter
var buffer = util.hexStringToArrayBuffer('68 65 6c 6c 6f 2c 20 77 6f 72 6c 64');
expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));

// : delimiter
var buffer = util.hexStringToArrayBuffer('68:65:6c:6c:6f:2c:20:77:6f:72:6c:64');
expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));

// - delimiter
var buffer = util.hexStringToArrayBuffer('68-65-6c-6c-6f-2c-20-77-6f-72-6c-64');
expect(new Uint8Array(buffer)).toEqual(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));

});
});

describe('nfc.transceive', function() {

it('should reject invalid input', function(done) {
var promise = nfc.transceive(42);

promise.then(
function resolve() {
throw new Error('Promise should not be resolved');
},
function reject(reason) {
expect(reason).toBe("Expecting an ArrayBuffer or String");
done();
}
)
});

});

};

exports.defineManualTests = function (contentEl, createActionButton) {
Expand Down
13 changes: 7 additions & 6 deletions www/phonegap-nfc.js
Expand Up @@ -513,7 +513,7 @@ var nfc = {
transceive: function(data) {
return new Promise(function(resolve, reject) {

let buffer;
var buffer;
if (typeof data === 'string') {
buffer = util.hexStringToArrayBuffer(data);
} else if (data instanceof ArrayBuffer) {
Expand Down Expand Up @@ -703,9 +703,10 @@ var util = {
function toHexString(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}
const typedArray = new Uint8Array(buffer);
var typedArray = new Uint8Array(buffer);
var array = Array.from(typedArray); // need to convert to [] so our map result is not typed
const parts = array.map(i => toHexString(i));
var parts = array.map(function(i) { return toHexString(i) });

return parts.join('');
},

Expand All @@ -729,16 +730,16 @@ var util = {
}

// check for some non-hex characters
const bad = hexString.match(/[G-Z\s]/i);
var bad = hexString.match(/[G-Z\s]/i);
if (bad) {
console.log('WARNING: found non-hex characters', bad);
}

// split the string into pairs of octets
const pairs = hexString.match(/[\dA-F]{2}/gi);
var pairs = hexString.match(/[\dA-F]{2}/gi);

// convert the octets to integers
const ints = pairs.map(s => parseInt(s, 16));
var ints = pairs.map(function(s) { return parseInt(s, 16) });

var array = new Uint8Array(ints);
return array.buffer;
Expand Down

0 comments on commit e6517e1

Please sign in to comment.