Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dawg-lookup

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dawg-lookup - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

lib/dawg.js

6

package.json
{
"name": "dawg-lookup",
"version": "2.0.0",
"version": "2.1.0",
"description": "Directed Acyclic Word Graph",
"main": "lib/index.js",
"bin": "lib/dawg.js",
"files": [
"lib",
"src"
],
"scripts": {

@@ -8,0 +12,0 @@ "test": "tools/run-tests"

67

src/dawg.ts

@@ -1,34 +0,59 @@

// Command-line interface for
#!/usr/bin/env node
/*
Command-line interface for dawg-lookup package.
Compress a file containing a dictionary of words into a packed
directed acyclic graph to be used with the 'dawg-lookup/ptrie'
decoder.
Usage:
dawg-lookup dictionary.txt > dictionary.dawg
To load the resulting DAWG for decoding (ES6 or TypeScript):
import { PTrie } from 'dawg-lookup/ptrie';
let dawg: string = <load compressed dictionary from ajax or file system>;
let ptrie = new PTrie(dawg);
if (ptrie.isWord('hello')) {
console.log("'Hello' is in the dictionary.");
}
*/
import * as fs from 'fs';
import { pack, stringStats } from './pack';
import { Trie } from './trie';
import { readFile } from './file-util';
// Command-line argument processing.
export function main(args: string[]) {
function main(args: string[]) {
if (args.length !== 1) {
throw new Error("Usage: string-pack-js <file.js>");
throw new Error("Usage: dawg-lookup dictionary.txt > dictionary.dawg");
}
packFile(args[0]);
compressDictionaryFile(args[0])
.then((packed) => {
process.stdout.write(packed + '\n');
});
}
// Pack a single file and write to '<name>.packed'.
export function packFile(name: string) {
console.log("Packing strings in: " + name);
// Pack a single file and write to standard output.
function compressDictionaryFile(path: string): Promise<string> {
return readFile(path)
.then((data) => {
let trie = new Trie(data);
let packed = trie.pack();
const contents = fs.readFileSync(name, 'utf8');
const stats = stringStats(contents);
console.error('Compressed ' + trie.wordCount + ' words.');
console.error('Input size: ' + data.length + ' bytes.');
console.error('Compressed size: ' + packed.length + ' bytes.');
console.log(`File length: ${contents.length}`);
console.log("All lengths:\n" + stats['lengths'].histogramReport());
console.log("Duplicates:\n" + stats['duplicates'].histogramReport());
const packed = pack(contents);
const bytesSaved = contents.length - packed.length;
const percentSaved = Math.floor(100 * bytesSaved / contents.length);
console.log(`Packed size: ${packed.length} (saved ${bytesSaved} or ` +
`${percentSaved}%).`);
fs.writeFileSync(name + '.packed', packed, 'utf8');
return packed;
});
}
// Call main() if run from command line (as opposed to required).
// Call main() if run from command line (as opposed to being required).
if (require.main === module) {

@@ -35,0 +60,0 @@ try {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc