forked from revtel/react-native-nfc-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NdefParser.js
64 lines (62 loc) · 1.52 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let arrayEqual = (a, b) => a && b && a.length === b.length && a.every((v, i) => v === b[i]);
let bytesToStr = bytes => bytes.reduce((acc, byte) => acc + String.fromCharCode(byte), '');
function parseUri(record) {
const RTD_URI_TYPE = [85];
const RTD_URI_SCHEMES = [
'',
'http://www.',
'https://www.',
'http://',
'https://',
'tel:',
'mailto:',
'ftp://anonymous:anonymous@',
'ftp://ftp.',
'ftps://',
'sftp://',
'smb://',
'nfs://',
'ftp://',
'dav://',
'news:',
'telnet://',
'imap:',
'rtsp://',
'urn:',
'pop:',
'sip:',
'sips:',
'tftp:',
'btspp://',
'btl2cap://',
'btgoep://',
'tcpobex://',
'irdaobex://',
'file://',
'urn:epc:id:',
'urn:epc:tag:',
'urn:epc:pat:',
'urn:epc:raw:',
'urn:epc:',
'urn:nfc:',
];
if (record && record.tnf === 1) {
if (record.type && arrayEqual(RTD_URI_TYPE, record.type)) {
let payload = record.payload,
scheme = RTD_URI_SCHEMES[payload[0]];
if (scheme !== undefined) {
try {
return {
uri: `${scheme}${bytesToStr(payload.slice(1))}`
}
} catch (err) {
return null;
}
}
}
}
return null;
}
export default {
parseUri
}