1.0.2 • Published 9 months ago

word-prefixes v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

word-prefixes

This package returns all the prefixes of a given word.

Installation

npm install word-prefixes

Usage

const getPrefixes = require('word-prefixes');

console.log(getPrefixes('prefix')); 
// Output: ['p', 'pr', 'pre', 'pref', 'prefi', 'prefix']

console.log(getPrefixes('a')); 
// Output: ['a']

console.log(getPrefixes('')); 
// Output: []

console.log(getPrefixes(123)); 
// Output: ['1', '12', '123']

// If a non-string or non-number is passed, an error will be thrown
try {
  console.log(getPrefixes({}));
} catch (error) {
  console.error(error.message); 
  // Output: Expected a string or a number
}

Sample Results

Input: 'prefix'

['p', 'pr', 'pre', 'pref', 'prefi', 'prefix']

Input: 'a'

['a']

Input: '' (empty string)

[]

Input: 123 (number)

['1', '12', '123']

Input: {} (non-string/number)

TypeError: Expected a string or a number

License

MIT