is-string-or-number-who-cares
v1.0.2
Published
The world's most advanced string or number detection library.
Downloads
341
Maintainers
Readme
is-string-or-number-who-cares
The world's most advanced string or number detection library.
Overview
After years of rigorous research and millions in funding (not really), we've finally solved the problem that JavaScript developers have been struggling with since 2009: checking if a value is a string or a number.
Built with cutting-edge typeof technology, this library leverages the latest advances in type detection to deliver results you never asked for.
Features
- Blazingly fast - It's just a
typeofcheck, what did you expect? - Zero dependencies - Because we couldn't afford any
- Enterprise-grade - Tested in production (by us, once)
- TypeScript ready - Just kidding, it's not
- Fully documented - You're reading it right now
- Number support - Now supporting numbers 1 through 20 (21-100 coming in Pro)
Installation
npm install is-string-or-number-who-caresUsage
Checking if a value is a string or number
const { isStringOrNumber } = require('is-string-or-number-who-cares');
isStringOrNumber('hello');
// { type: 'string', message: "It's a string, congrats 🎉" }
isStringOrNumber(7);
// { type: 'number', message: "It's a number, you're amazing 🏆" }
isStringOrNumber(42);
// { type: 'unsupported', message: "Number 42 is not supported yet. We only verify numbers from 1 to 20. Upgrade to Pro for 21-100." }
isStringOrNumber(true);
// { type: 'garbage', message: "Not a string or number. Go back to school." }Converting strings to numbers
Our proprietary stringToNumber algorithm uses a revolutionary approach: it tells you it can't convert.
const { stringToNumber } = require('is-string-or-number-who-cares');
stringToNumber('123');
// { result: NaN, message: '"123" is a string, genius. To convert it needs to be a number.' }
stringToNumber(42);
// { result: 42, message: "It's already a number (42). Are you testing me?" }
stringToNumber(null);
// { result: NaN, message: 'That is nothing. Give up.' }API Reference
isStringOrNumber(value)
Determines whether the provided value is a string, a number, or garbage.
| Parameter | Type | Description |
|-----------|------|-------------|
| value | any | The value to check. Try not to pass null. |
Returns: An object with type and message properties. The message is mandatory because we care about your feelings.
Supported numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20. Any other number will return an "unsupported" type. This is a deliberate design choice, not laziness.
stringToNumber(value)
Attempts to convert a value to a number. Spoiler: it won't.
| Parameter | Type | Description |
|-----------|------|-------------|
| value | any | The value to convert. Good luck. |
Returns: An object with result (always NaN if you passed a string) and a message explaining why it's your fault.
Why?
Because typeof is too hard to type. This saves you 6 whole characters.
Contributing
Please don't. This is perfect.
License
WTFPL - Do What The Fuck You Want To Public License
