forked from revtel/react-native-nfc-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NdefParser.js
38 lines (35 loc) · 1.01 KB
/
NdefParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const textHelper = require("./ndef-lib/ndef-text");
const uriHelper = require("./ndef-lib/ndef-uri");
const arrayEqual = (a, b) => a && b && a.length === b.length && a.every((v, i) => v === b[i]);
function parseText(record) {
const RTD_TEXT_TYPE = [0x54];
if (record && record.tnf === 1) {
if (record.type && arrayEqual(RTD_TEXT_TYPE, record.type)) {
try { // only handle utf8 for now
return textHelper.decodePayload(record.payload);
} catch (ex) {
return null;
}
}
}
return null;
}
function parseUri(record) {
const RTD_URI_TYPE = [0x55];
if (record && record.tnf === 1) {
if (record.type && arrayEqual(RTD_URI_TYPE, record.type)) {
try {
return {
uri: uriHelper.decodePayload(record.payload)
}
} catch (err) {
return null;
}
}
}
return null;
}
export default {
parseUri,
parseText,
}