thenativeweb / boolean Goto Github PK
View Code? Open in Web Editor NEWboolean converts lots of things to boolean.
License: MIT License
boolean converts lots of things to boolean.
License: MIT License
The recent v3.1.3 has a set dependency for node >= 16.7.0. As it's a patch release, this breaks other packages dependent on it without a specified exact version number (in our case, pm2) when using a lower node version.
Line 5 in f35f011
Is there a good reason to trim the string ? Could the following regex be used instead ?
return /^\s*(true|t|yes|y|1)\s*$/i.test(value);
Would be nice!
The following regex causes Firefox to throw "SyntaxError: invalid regexp group":
Line 3 in bbad0b1
the main
entry is pointing to "main": "build/lib/boolean.js",
however the only available file is "main": "build/src/boolean.js",
Hi,
Can you add the string "ON" and "on" to convert this to true?
Or is there a reason it shouldn't be?
Thanks!
I would love to see a function that tells you if an object/string/whatever is one of the "booleanable" options.
For example isBooleanable() would return True for the values you consider a truth or false: true, false, 'Y', 'N', '1', 0, 'on', 'off', etc...
But when passing something that isn't one of the covered true/false items, it would return False: 'computer', '2022-02-15T11:37:00-08:00', etc...
That would allow you to check if an object is something that should evaluate to a true/false before calling boolean() and potentially getting False as the default result.
There is really no reason to drop v8 support over a single line of code:
https://github.com/thenativeweb/boolean/blob/master/lib/boolean.ts#L3
It creates compatibility issues such as this:
v8 is still Maintenance LTS.
Nice package. However, I was testing this package for my test cases and if failed for "TRUE " or "true ". trim might be required.
This library is much more permissive than the readme claims.
Take this code as an example
var boolean = require('boolean')
console.log(boolean([false]))
console.log(boolean({}))
console.log(boolean(001))
console.log(boolean(100))
console.log(boolean('negative'))
console.log(boolean('hellyo'))
console.log(boolean('noyesno'))
console.log(boolean('321'))
All of these expressions will return true
Any object which is truthy and not a string will return true
Any string that contains a substring of 't', 'y', or 'yes' will return true
Any string that begins with the word 'true' or ends with the number 1 will return true
Line 3 in 329dac4
Lookbehind assertions got introduced in early 2018 and browsers like FF still don't support it. In order for this library to be more acceptable to work with all the major browsers, I would recommend to use look ahead assertions at this point in time.
This works on recent versions of Chrome and Safari. I haven't tested on IE.
Ref: https://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent#
Failed to compile.
./node_modules/boolean/build/lib/boolean.js
SyntaxError: /Users/gajus/dev/applaudience/applaudience-explorer-web-app/node_modules/boolean/build/lib/boolean.js: Expected atom at position 2
^(?<truthy>t
^
^C
While I know it is advised not to use primitive object wrappers, you might still want to look into this:
const truthy = new String('true')
console.log(boolean(truthy)); // => false
A more robust approach would be doing something like this:
switch (Object.prototype.toString.call(value)) {
case '[object String]':
return [ 'true', 't', 'yes', 'y', 'on', '1' ].includes(value.trim().toLowerCase());
case '[object Number]':
return value.valueOf() === 1;
case '[object Boolean]':
return value.valueOf();
default:
return false;
}
Recently I've started working on a project that's badly outdated and "boolean" is "^0.2.0" ... I would like to 'just upgrade it' but not sure what the implications are.
Would be good if the changelog had some notes on any breaking changes.
I suppose it was done for some reason, but this reason is very unclear.
Is it bug or feature?
boolean(1) // true
boolean(2) // false
If its a feature, why is it so?
You can see demo on jsFiddle.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.