isoneword
v1.1.7
Published
Checks if a value is a string containing exactly one word.
Maintainers
Readme
Usage
All time downloads:
Checks if a value is a string containing exactly one word.
const isOneWord = require('isoneword');
console.log(isOneWord('hello')); // true
console.log(isOneWord(' hello ')); // true (ignores leading/trailing space)
console.log(isOneWord('hello world')); // false
console.log(isOneWord('')); // false
console.log(isOneWord(123)); // falseHere are additional usage examples covering different module systems (CommonJS and ES Modules) as well as various edge cases, including special characters, punctuation, and multi-line strings.
ES Modules (ESM) Usage
If the target repository uses "type": "module" in package.json or a modern bundler like Vite, Webpack, or Next, import the package using standard import syntax:
import isOneWord from 'isoneword';
console.log(isOneWord('OpenAI')); // true
Comprehensive Validation Examples
The following test cases demonstrate how the package handles edge cases, numbers, punctuation, and hidden whitespace characters.
const isOneWord = require('isoneword');
// --- Standard Single Words ---
console.log(isOneWord('data')); // true
console.log(isOneWord('supercalifragilisticexpialidocious')); // true
// --- Spaces and Trimming ---
console.log(isOneWord(' test ')); // true (surrounding spaces are ignored)
console.log(isOneWord('a b')); // false (internal space creates two words)
console.log(isOneWord(' ')); // false (empty space strings return false)
// --- Special Characters and Punctuation ---
// Non-whitespace characters are treated as part of the single word string
console.log(isOneWord('semi-colon')); // true (hyphens contain no whitespace)
console.log(isOneWord('user_name')); // true (underscores contain no whitespace)
console.log(isOneWord('word!')); // true (exclamation points contain no whitespace)
console.log(isOneWord('$100')); // true
// --- Hidden Whitespace (Tabs and Newlines) ---
console.log(isOneWord('word\tanother')); // false (contains a tab character)
console.log(isOneWord('first\nsecond')); // false (contains a newline character)
console.log(isOneWord('line1\rline2')); // false (contains a carriage return character)
// --- Non-String Data Types ---
// Any non-string type will safely return false instead of throwing an error
console.log(isOneWord(null)); // false
console.log(isOneWord(undefined)); // false
console.log(isOneWord(42)); // false
console.log(isOneWord(['word'])); // false
console.log(isOneWord({ word: true })); // false
Integrating with Array Methods
Because the function returns a strict boolean, this can be passed directly into native js array methods like Array.prototype.filter() or Array.prototype.every().
const isOneWord = require('isoneword');
const tags = ['coding', 'open source', 'npm'];
// Filter a list to find only single-word tags
const singleWordTags = tags.filter(isOneWord);
console.log(singleWordTags);
// Output: ['coding', 'npm']
// Check if an entire array consists of single words
const productIds = ['sku123', 'sku456', 'sku 789'];
const allValidIds = productIds.every(isOneWord);
console.log(allValidIds);
// Output: false (because 'sku 789' contains a space)
