This commit is contained in:
2026-10-22 16:06:51 +07:00
commit 25158e780f
2897 changed files with 569062 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
/**
* Pembacaan data input (Node).
*/
'use strict';
const fs = require('fs');
const LabelData = require('./data');
function readFile(filePath) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath, 'utf8', function (err, content) {
if (err) { reject(err); return; }
resolve(content);
});
});
}
function readStdin() {
return new Promise(function (resolve, reject) {
let data = '';
if (process.stdin.isTTY) {
resolve('');
return;
}
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) { data += chunk; });
process.stdin.on('end', function () { resolve(data); });
process.stdin.on('error', reject);
});
}
/**
* Muat daftar item label dari file (JSON / TXT / CSV sederhana) atau stdin.
* @param {string} file path file, atau "-" untuk stdin
* @returns {Promise<Array>}
*/
async function loadItems(file) {
const text = file === '-' ? await readStdin() : await readFile(file);
return LabelData.parseText(text);
}
module.exports = {
loadItems: loadItems,
readFile: readFile,
readStdin: readStdin
};