├── .github └── dependabot.yml ├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── example ├── .babelrc ├── .gitignore ├── package.json └── src │ └── example1.js ├── lib ├── index.ts └── word-probability.ts ├── package.json ├── test └── index.test.ts └── tsconfig.json /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | timezone: Asia/Tokyo 8 | open-pull-requests-limit: 99 9 | reviewers: 10 | - nwtgck 11 | assignees: 12 | - nwtgck 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /package-lock.json 3 | /dist 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # /dist/resources is used in built program 2 | /resources 3 | /example 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # (from: https://github.com/codecov/example-node/blob/master/.travis.yml) 2 | language: node_js 3 | sudo: false 4 | node_js: 5 | - "12" 6 | - "10" 7 | - "8" 8 | install: 9 | - npm install 10 | script: 11 | - npm test 12 | # Test example 13 | - npm run build && cd example && npm i && npm run examples 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ryo Ota 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fakelish 2 | 3 | [![npm](https://img.shields.io/npm/v/fakelish.svg)](https://www.npmjs.com/package/fakelish) 4 | [![Build Status](https://travis-ci.com/nwtgck/fakelish-npm.svg?token=TuxNpqznwwyy7hyJwBVm&branch=develop)](https://travis-ci.com/nwtgck/fakelish-npm) 5 | 6 | Fake English word generator 7 | 8 | ## Try on Web Browser 9 | 10 | (repo: ) 11 | 12 | ## Install 13 | 14 | ```bash 15 | npm i -S fakelish 16 | ``` 17 | 18 | ## Usage 19 | 20 | Here is an usage in JavaScript. 21 | 22 | ```js 23 | // Import fakelish 24 | const fakelish = require("fakelish"); 25 | 26 | // Min and max Lengths of fake words 27 | const minLen = 7; 28 | const maxLen = 11; 29 | 30 | (async ()=>{ 31 | // Generate 20 fake words 32 | for(let i = 0; i < 20; i++) { 33 | // Generate a fake word 34 | const fakeWord = await fakelish.generateFakeWord(minLen, maxLen); 35 | // Print the capitalized fake word 36 | console.log(capitalize(fakeWord)); 37 | } 38 | })(); 39 | 40 | // Capitalize string 41 | function capitalize(str) { 42 | return str.charAt(0).toUpperCase() + str.substring(1); 43 | } 44 | ``` 45 | ([example/src/example1.js](example/src/example1.js)) 46 | 47 | 48 | The output should be like the following. 49 | 50 | ``` 51 | Schyperant 52 | Imposillack 53 | Tioughters 54 | Schgruids 55 | Viestios 56 | Mundrial 57 | Unkalphast 58 | Mireock 59 | Babrilly 60 | Formalated 61 | Gimbrawer 62 | Trident 63 | Compler 64 | Ligical 65 | Deprene 66 | Ocoarriete 67 | Raxalluffic 68 | Pecties 69 | Daeorhons 70 | Herbalve 71 | ``` 72 | 73 | ## For Go language and CLI 74 | GitHub Repository: 75 | -------------------------------------------------------------------------------- /example/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "targets": { 5 | "node": "current" 6 | } 7 | }] 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | /package-lock.json 2 | /node_modules 3 | /dist 4 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fakelish-example", 3 | "version": "1.0.0", 4 | "description": "Example of fakelish", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1", 7 | "build": "babel src -d dist", 8 | "examples": "npm run build && node dist/example1.js" 9 | }, 10 | "author": "Ryo Ota", 11 | "dependencies": { 12 | "fakelish": "file:.." 13 | }, 14 | "devDependencies": { 15 | "babel-cli": "^6.26.0", 16 | "babel-preset-env": "^1.7.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /example/src/example1.js: -------------------------------------------------------------------------------- 1 | // Import fakelish 2 | const fakelish = require("fakelish"); 3 | 4 | // Min and max Lengths of fake words 5 | const minLen = 7; 6 | const maxLen = 11; 7 | 8 | (async ()=>{ 9 | // Generate 20 fake words 10 | for(let i = 0; i < 20; i++) { 11 | // Generate a fake word 12 | const fakeWord = await fakelish.generateFakeWord(minLen, maxLen); 13 | // Print the capitalized fake word 14 | console.log(capitalize(fakeWord)); 15 | } 16 | })(); 17 | 18 | // Capitalize string 19 | function capitalize(str) { 20 | return str.charAt(0).toUpperCase() + str.substring(1); 21 | } 22 | -------------------------------------------------------------------------------- /lib/index.ts: -------------------------------------------------------------------------------- 1 | import {wordProbability} from './word-probability'; 2 | 3 | /** 4 | * Generate a fake word whose length is unexpected 5 | * 6 | * @param maxSeqN 7 | * @param randomGenerator 8 | */ 9 | export function generateFakeWordWithUnexpectedLength(maxSeqN: number = 4, randomGenerator: () => number = Math.random): string { 10 | let ch = "^"; 11 | let fakeWord = ""; 12 | let chrs: string[] = []; 13 | while(ch !== "END") { 14 | chrs = [...chrs, ch]; 15 | if (chrs.length > maxSeqN) { 16 | chrs = chrs.slice(1); 17 | } 18 | let nextAccumedProbs: [string, number][]; 19 | let n = 0; 20 | do { 21 | const str = chrs.slice(n).join(""); 22 | nextAccumedProbs = wordProbability[str]; 23 | n += 1 24 | } while (nextAccumedProbs === undefined && n < chrs.length); 25 | 26 | let nextCh: string = ""; 27 | const r = randomGenerator(); 28 | for(let x of nextAccumedProbs) { 29 | const candidateNextCh = x[0]; 30 | const prob = x[1]; 31 | if(r <= prob) { 32 | nextCh = candidateNextCh; 33 | break 34 | } 35 | } 36 | if(nextCh !== "END") { 37 | fakeWord += nextCh; 38 | } 39 | ch = nextCh; 40 | } 41 | return fakeWord; 42 | } 43 | 44 | /** 45 | * Generate a fake word by the specific length 46 | * 47 | * @param length 48 | * @param maxSeqN 49 | * @param randomGenerator 50 | */ 51 | export async function generateFakeWordByLength(length: number, maxSeqN: number = 4, randomGenerator: () => number = Math.random): Promise { 52 | let fakeWord: string = ""; 53 | while(fakeWord.length != length) { 54 | fakeWord = await new Promise((resolve)=>{ 55 | resolve(generateFakeWordWithUnexpectedLength(maxSeqN, randomGenerator)); 56 | }); 57 | } 58 | return fakeWord 59 | } 60 | 61 | /** 62 | * Generate a fake word by the specific min and max lengths 63 | * @param minLength 64 | * @param maxLength 65 | * @param maxSeqN 66 | * @param randomGenerator 67 | */ 68 | export async function generateFakeWord(minLength: number = 4, maxLength: number = 9, maxSeqN: number = 2, randomGenerator: () => number = Math.random): Promise { 69 | let fakeWord: string = ""; 70 | while(!(minLength <= fakeWord.length && fakeWord.length <= maxLength)) { 71 | fakeWord = await new Promise((resolve)=>{ 72 | resolve(generateFakeWordWithUnexpectedLength(maxSeqN, randomGenerator)); 73 | }); 74 | } 75 | return fakeWord 76 | } 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fakelish", 3 | "version": "0.3.0", 4 | "description": "Fake English word generator", 5 | "main": "dist/lib/index.js", 6 | "types": "dist/lib/index.d.ts", 7 | "scripts": { 8 | "build": "tsc", 9 | "prepare": "npm run build", 10 | "test": "mocha --require ts-node/register test/**/*.ts" 11 | }, 12 | "keywords": [ 13 | "word", 14 | "fake", 15 | "generator" 16 | ], 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/nwtgck/fakelish-npm" 20 | }, 21 | "author": "Ryo Ota", 22 | "license": "MIT", 23 | "devDependencies": { 24 | "@types/mocha": "^9.0.0", 25 | "@types/node": "^10.9.4", 26 | "@types/power-assert": "^1.5.0", 27 | "espower-typescript": "^9.0.0", 28 | "mocha": "^9.1.3", 29 | "power-assert": "^1.4.4", 30 | "ts-node": "^10.0.0", 31 | "typescript": "^4.3.4" 32 | }, 33 | "dependencies": {} 34 | } 35 | -------------------------------------------------------------------------------- /test/index.test.ts: -------------------------------------------------------------------------------- 1 | import {generateFakeWordWithUnexpectedLength, generateFakeWordByLength, generateFakeWord} from '../lib'; 2 | import * as assert from 'power-assert'; 3 | 4 | describe('generateFakeWordByLength', () => { 5 | it('should return fake world with length', async () => { 6 | const length: number = 6; 7 | const fakeWord: string = await generateFakeWordByLength(6); 8 | assert.equal(fakeWord.length, length); 9 | }); 10 | 11 | it('should return fake world with min and max lengths', async () => { 12 | const minLength: number = 4; 13 | const maxLength: number = 9; 14 | for(let i = 0; i < 100; i++) { 15 | const fakeWord: string = await generateFakeWord(minLength, maxLength); 16 | assert.equal(minLength <= fakeWord.length && fakeWord.length <= maxLength, true); 17 | } 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "strict": true, 5 | "noImplicitReturns": true, 6 | "removeComments": true, 7 | "preserveConstEnums": true, 8 | "sourceMap": true, 9 | "declaration": true, 10 | "target": "es5", 11 | "lib": ["es2015"], // Without this, an error occurs: TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. 12 | "outDir": "dist", 13 | "rootDir": "./" 14 | }, 15 | "include": [ 16 | "lib/**/*", 17 | "test/**/*" 18 | ], 19 | "exclude": [ 20 | "node_modules/*", 21 | "example/*" 22 | ] 23 | } 24 | --------------------------------------------------------------------------------