33 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| "use strict";
 | |
| var __importDefault = (this && this.__importDefault) || function (mod) {
 | |
|     return (mod && mod.__esModule) ? mod : { "default": mod };
 | |
| };
 | |
| Object.defineProperty(exports, "__esModule", { value: true });
 | |
| const fast_fuzzy_1 = require("fast-fuzzy");
 | |
| const fs_1 = __importDefault(require("fs"));
 | |
| // Get list source from args
 | |
| // ARGS: type source
 | |
| // Then we read query from stdin to not restart indexing & the like for each keystroke
 | |
| let data = [];
 | |
| if (process.argv[2] === 'fs') {
 | |
|     if (process.argv[3].includes('.json')) {
 | |
|         data = JSON.parse('' + fs_1.default.readFileSync(process.argv[3]));
 | |
|     }
 | |
|     else if (process.argv[3].includes('.txt')) {
 | |
|         data = ('' + fs_1.default.readFileSync(process.argv[3])).split(',');
 | |
|     }
 | |
|     else if (fs_1.default.statSync(process.argv[3]).isDirectory()) {
 | |
|         data = fs_1.default.readdirSync(process.argv[3]);
 | |
|     }
 | |
| }
 | |
| else if (process.argv[2] === 'arg') {
 | |
|     data = process.argv[3].split(',');
 | |
| }
 | |
| else {
 | |
|     throw new Error('Invalid argument at position 1. Can be either fs or arg, not ' + process.argv[2]);
 | |
| }
 | |
| process.stdin.on("data", (query) => {
 | |
|     // On stdin submit (which the other client will have to support) process data
 | |
|     console.log((0, fast_fuzzy_1.search)(query.toString(), data));
 | |
| });
 |