Giter VIP home page Giter VIP logo

random-words's Introduction

random-words

Generate one or more common English words

random-words generates random words for use as sample text. We use it to generate random blog posts when testing Apostrophe.

Cryptographic-quality randomness is NOT the goal, as speed matters for generating sample text and security does not. As such, Math.random() is used in most cases.

The seed option can be used with the generate function for situations that require deterministic output. When given the same seed with the same input, generate() will yield deterministic results, in regards to both actual word selection and the number of words returned (when using min and max). The underlying implementation of this option utilizes the seedrandom package as a replacement for Math.random().

The count function can be used to calculate the total number of words in the word list that meet the specified minimum and maximum length criteria.

Installation:

npm install random-words

Examples:

import { generate, count } from "random-words";

console.log(generate());
//output: 'army'

console.log(generate(5));
//output: ['army', 'beautiful', 'became', 'if', 'actually']

console.log(generate({ minLength: 2 }));
//output: 'hello'

console.log(generate({ maxLength: 6 }));
//output: 'blue'

console.log(generate({ minLength: 5, maxLength: 5 }));
//output : 'world'

console.log(generate({ minLength: 11, maxLength: 10000 })); //maxLength limited to the longest possible word
//output: 'environment'

console.log(generate({ minLength: 10000, maxLength: 5 })); //minLength limited to the maxLength
//output: 'short'

console.log(generate({ min: 3, max: 10 }));
//output: ['became', 'arrow', 'article', 'therefore']

console.log(generate({ exactly: 2 }));
//output: ['beside', 'between']

console.log(generate({ min: 2, max: 3, seed: "my-seed" }));
//output: ['plenty', 'pure']

// this call will yield exactly the same results as the last since the same `seed` was used and the other inputs are identical
console.log(generate({ min: 2, max: 3, seed: "my-seed" }));
//output: ['plenty', 'pure']

console.log(generate({ exactly: 5, join: " " }));
//output: 'army beautiful became if exactly'

console.log(generate({ exactly: 5, join: "" }));
//output: 'armybeautifulbecameifexactly'

console.log(generate({ exactly: 2, minLength: 4 }));
//output: ['atom', 'window']

console.log(generate({ exactly: 5, maxLength: 4 }));
//output: ['army', 'come', 'eye', 'five', 'fur']

console.log(generate({ exactly: 2, minLength: 3, maxLength: 3 }));
//output: ['you, 'are']

console.log(generate({ exactly: 3, minLength: 5, maxLength: 100000 }));
//output: ['understanding', 'should', 'yourself']

console.log(generate({ exactly: 5, wordsPerString: 2 }));
//output: [ 'salt practical', 'also brief', 'country muscle', 'neighborhood beyond', 'grew pig' ]

console.log(generate({ exactly: 5, wordsPerString: 2, separator: "-" }));
//output: [ 'equator-variety', 'salt-usually', 'importance-becoming', 'stream-several', 'goes-fight' ]

console.log(
  generate({
    exactly: 5,
    wordsPerString: 2,
    formatter: (word) => word.toUpperCase(),
  })
);
//output: [ 'HAVING LOAD', 'LOST PINE', 'GAME SLOPE', 'SECRET GIANT', 'INDEED LOCATION' ]

console.log(
  generate({
    exactly: 5,
    wordsPerString: 2,
    formatter: (word, index) => {
      return index === 0
        ? word.slice(0, 1).toUpperCase().concat(word.slice(1))
        : word;
    },
  })
);
//output: [ 'Until smoke', 'Year strength', 'Pay knew', 'Fallen must', 'Chief arrow' ]

console.log(count());
//output: 1952

console.log(count({ minLength: 5 }));
//output: 1318 

console.log(count({ maxLength: 7 }));
//output: 1649

console.log(count({ minLength: 5, maxLength: 7 }));
//output: 1015

random-words's People

Contributors

abea avatar andreigec avatar bodonkey avatar boutell avatar crlh avatar iamparadoxdotexe avatar mattveraldi avatar nellfs avatar orrshalev avatar prateek-budhiraja avatar scoombe avatar stretchkennedy avatar thechickennagget avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

random-words's Issues

IE10 error "Expected: ';'"

We support IE10 because we have to.
IE11 is fine; however, IE10 throws SCRIPT1004: Expected ';' on line 260 which is the ES6 let keyword. I see other places in the code that use var. Could we just use that?
I was using 0.0.1 heretofore with no problem; however, it does not support maxLength.

capturerw

Request - generate words starting with letter

Would love to see an option that will generate random words starting with a specific letter (parameter a-z).

Something like

let generateRandomWordStartingWith = wordList.filter(function (word) {
  return word[0].toLowerCase() === 'a';
});

Unable to use minLength

To Reproduce

Step by step instructions to reproduce the behavior:

  1. Import random-words in a *.ts file with import randomWords from "random-words";
  2. Attempt to call randomWords with options: randomWords({ exactly: 1, minLength: 4, maxLength: 8 });
  3. See typescript compile error

Expected behavior

Function should be allowed to run

Describe the bug

Here is the entire error message:

No overload matches this call.
  Overload 1 of 3, '(count: number): string[]', gave the following error.
    Argument of type '{ exactly: number; minLength: number; maxLength: number; }' is not assignable to parameter of type 'number'.
  Overload 2 of 3, '(options: WordsOptions): string[]', gave the following error.
    Argument of type '{ exactly: number; minLength: number; maxLength: number; }' is not assignable to parameter of type 'WordsOptions'.
      Object literal may only specify known properties, and 'minLength' does not exist in type 'WordsOptions'.
  Overload 3 of 3, '(options: JoinedWordsOptions): string', gave the following error.
    Argument of type '{ exactly: number; minLength: number; maxLength: number; }' is not assignable to parameter of type 'JoinedWordsOptions'.
      Object literal may only specify known properties, and 'minLength' does not exist in type 'JoinedWordsOptions'.

Details

Version of Node.js:
v18.15.0

Server Operating System:
Windows 11

Please update the version

Recent changes have occurred where the developers fixed a spelling error on index.d.ts for TypeScript users. However, since there has been not new version after this was pushed out when TypeScript developers install the package, they miss out on this change and experienced issues with the TypeScript spelling error. The workaround for this is to add the following in your package.json:

"random-words": "git://github.com/apostrophecms/random-words.git#e05e942f67387e06b2ac676450a6e41ebefab751"

The hashtag at the end indicates the latest commit ID.

To the devs, please push out a version so TypeScript developers can get updated with the latest commits.

Thanks.

Syntax error with release 1.3.0

const random = options?.seed ? new seedrandom(options.seed) : null;
^

SyntaxError: Unexpected token .
at Module._compile (internal/modules/cjs/loader.js:760:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object. (/opt/hostedtoolcache/node/11.15.0/x64/lib/node_modules/imp-central-impt-eaton/lib/util/TestSession.js:45:21)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
PID for log retrieval: 4988

Exact size of random word

Thank you for library!

Is there any way for generating word with a predefined size of word length?
As far as I can see, it could be implemented with minLength parameter, for example:

randomWords({minLength: 4, maxLength: 4})

Export CommonJS version

The problem to solve

Is your feature request related to a problem you are experiencing that is not a bug? Please describe.

I want to use the project, but the project I'm working on does not support ESModules.

Proposed solution

It'd be nice to have a /commonjs counterpart to the current implementation.

Alternatives

Use something like babel to output a CommonJS version before publishing it

Publish latest version to registry

Hi,

I was encountering security issues when auditing because of the old Mocha version as a productive dependency.
I saw that you recently updated the Mocha version used and in 2020 also changed it to a dev dependency, but it seems like you did not publish it to the registry since then.
May I ask you to publish it so I can receive your patch via NPM?

Transpiling

Nice library.

FYI, its usage in browsers is hampered by the fact that there are some things needed transpile in older browser (let was the main thing I noticed).

I got around it by manually specifying transpiling in my webpack config, but you might consider adding some transpiling at the module level.

Words being made plural that should not be

I've noticed an issue where words become plural that shouldnt be plural, and it makes the word unusable. I'm assuming part of the code randomly tacks an s onto a word, for more variety, but this doesnt always work. For instance the once i've seen quite a bit of is "of" becoming "ofs".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.