first
This commit is contained in:
+168
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* Parsing data label (UMD, tanpa dependensi) sehingga format input yang
|
||||
* sama dipakai oleh CLI Node maupun aplikasi HTML.
|
||||
*
|
||||
* Format baris teks : "KODE BARU | SN"
|
||||
* - pemisah SN boleh "|" atau TAB
|
||||
* - "SN: 12345678" juga diterima, label "SN:" dibuang otomatis
|
||||
* - baris kosong dan baris komentar "#..." dilewati
|
||||
* - kode gabungan "ONT-191026-3BA3" otomatis ditampilkan
|
||||
* menjadi dua baris: "ONT-1910" / "26-3BA3"
|
||||
*
|
||||
* Format JSON : [{ "code": "ONT-1910", "unique": "26-5BD9", "sn": "12345678" }]
|
||||
* - kunci alternatif: payload/kode, unique/uniqueCode, sn/serial
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory();
|
||||
} else {
|
||||
root.LabelData = factory();
|
||||
}
|
||||
})(typeof self !== 'undefined' ? self : this, function () {
|
||||
'use strict';
|
||||
|
||||
function stripQuotes(value) {
|
||||
return String(value).replace(/^["']/, '').replace(/["']$/, '').trim();
|
||||
}
|
||||
|
||||
/** Bersihkan nilai SN: buang prefix "SN:" dan tanda kutip. */
|
||||
function normalizeSn(value) {
|
||||
if (value === undefined || value === null) { return ''; }
|
||||
var sn = String(value).trim();
|
||||
sn = sn.replace(/^SN\s*:\s*/i, '');
|
||||
sn = stripQuotes(sn);
|
||||
return sn;
|
||||
}
|
||||
|
||||
/** Pecah satu baris menjadi payload (isi QR) dan SN. */
|
||||
function parseLine(line) {
|
||||
var raw = String(line === undefined || line === null ? '' : line);
|
||||
var text = raw.replace(/^\uFEFF/, '').trim();
|
||||
if (!text) { return null; }
|
||||
if (text.charAt(0) === '#') { return null; }
|
||||
|
||||
var sn = '';
|
||||
var cut = text.indexOf('|');
|
||||
if (cut >= 0) {
|
||||
sn = text.slice(cut + 1);
|
||||
text = text.slice(0, cut);
|
||||
} else {
|
||||
var tab = text.indexOf('\t');
|
||||
if (tab >= 0) {
|
||||
sn = text.slice(tab + 1);
|
||||
text = text.slice(0, tab);
|
||||
}
|
||||
}
|
||||
|
||||
var payload = text.trim();
|
||||
if (!payload) { return null; }
|
||||
|
||||
return buildItem(payload, normalizeSn(sn), raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pisahkan payload menjadi kode utama dan kode unik.
|
||||
* - "ONT-0910 26-5BD9" -> dipecah pada spasi terakhir.
|
||||
* - "ONT-191026-3BA3" -> kode gabungan 8 karakter + kode unik
|
||||
* "26-3BA3"; payload QR tetap memakai teks lengkap tanpa spasi.
|
||||
*/
|
||||
function splitPayload(payload) {
|
||||
var text = String(payload || '');
|
||||
var cut = text.lastIndexOf(' ');
|
||||
if (cut > 0) {
|
||||
return { main: text.slice(0, cut), unique: text.slice(cut + 1) };
|
||||
}
|
||||
var compact = /^([A-Za-z]{3}-\d{4})(\d{2}-[A-Za-z0-9]{4})$/.exec(text);
|
||||
if (compact) {
|
||||
return { main: compact[1], unique: compact[2] };
|
||||
}
|
||||
return { main: text, unique: '' };
|
||||
}
|
||||
|
||||
function buildItem(payload, sn, source) {
|
||||
var parts = splitPayload(payload);
|
||||
return {
|
||||
payload: payload,
|
||||
sn: sn || '',
|
||||
main: parts.main,
|
||||
unique: parts.unique,
|
||||
snText: 'SN: -' + (sn ? ' ' + sn : ''),
|
||||
source: source === undefined ? payload : source
|
||||
};
|
||||
}
|
||||
|
||||
function itemFromObject(raw) {
|
||||
var code = raw.code || raw.payload || raw.kode || raw.item || '';
|
||||
var unique = raw.unique || raw.uniqueCode || raw.kode_unik || raw.serial_code || '';
|
||||
var sn = normalizeSn(raw.sn || raw.serial || raw.SN || raw.snCode || '');
|
||||
code = String(code).trim();
|
||||
if (!code) { return null; }
|
||||
var payload = unique ? code + ' ' + String(unique).trim() : code;
|
||||
var item = buildItem(payload, sn, payload);
|
||||
if (unique) {
|
||||
item.main = code;
|
||||
item.unique = String(unique).trim();
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
function itemsFromJson(data) {
|
||||
var list;
|
||||
if (Array.isArray(data)) {
|
||||
list = data;
|
||||
} else if (data && Array.isArray(data.items)) {
|
||||
list = data.items;
|
||||
} else if (data && Array.isArray(data.labels)) {
|
||||
list = data.labels;
|
||||
} else if (data && Array.isArray(data.data)) {
|
||||
list = data.data;
|
||||
} else {
|
||||
list = [data];
|
||||
}
|
||||
|
||||
var items = [];
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var raw = list[i];
|
||||
var item = null;
|
||||
if (typeof raw === 'string' || typeof raw === 'number') {
|
||||
item = parseLine(String(raw));
|
||||
} else if (raw && typeof raw === 'object') {
|
||||
item = itemFromObject(raw);
|
||||
}
|
||||
if (item) { items.push(item); }
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** Parse teks mentah (JSON atau daftar baris) menjadi daftar item label. */
|
||||
function parseText(raw) {
|
||||
var text = String(raw === undefined || raw === null ? '' : raw);
|
||||
var trimmed = text.replace(/^\uFEFF/, '').trim();
|
||||
if (!trimmed) { return []; }
|
||||
|
||||
var first = trimmed.charAt(0);
|
||||
if (first === '[' || first === '{') {
|
||||
try {
|
||||
return itemsFromJson(JSON.parse(trimmed));
|
||||
} catch (err) {
|
||||
throw new Error('Input JSON tidak valid: ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
var lines = text.split(/\r?\n/);
|
||||
var items = [];
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var item = parseLine(lines[i]);
|
||||
if (item) { items.push(item); }
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
return {
|
||||
parseText: parseText,
|
||||
parseLine: parseLine,
|
||||
splitPayload: splitPayload,
|
||||
normalizeSn: normalizeSn,
|
||||
buildItem: buildItem
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user