
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Wrap and validate optional values
This package provides wrappers to check inputs and provide fallbacks. There is a general some
wrapper to check if a value is defined or not, as well as a handful of others for various primitives. The chained syntax is more verbose than the standard JS way (i.e. var str = opts.str || 'default'
), but it is more solid and predictable.
var { some } = require('stdopt')
some('some data').value() // => 'some data'
some(null).or('other data').value() // => 'other data'
some(null).or(undefined).value() // => throws error
// string primitive
var { string } = require('stdopt')
string('some text').value() // => 'some text'
string(true).or('other text').value() // => 'other text'
string(5).value() // => '5'
// number primitive
var { number } = require('stdopt')
number(5).value() // => 5
number(true).or(7).value() // => 7
number('11').value() // => 11
// boolean primitive
var { boolean } = require('stdopt')
boolean(false).value() // => false
boolean(null).or(true).value() // => true
boolean('True').value() // => true
boolean('fAlSe').value() // => false
// hash primitive
var { hash } = require('stdopt')
hash({stuff: true}).value() // => {stuff: true}
hash(true).or({other: false}).value() // => {other: false}
hash([1, 2, 3]).value() // => throws error
// list primitive
var { list } = require('stdopt')
list([1, 2, 3]).value() // => [1, 2, 3]
list('one').value() // => ['one']
list(null).or([4, 5, 6]).value() // => [4, 5, 6]
list({0: 'stuff', length: 1}).value() // => ['stuff']
You can also create your own custom primitives, by using the Opt
class and defining a custom static parse
method.
var Opt = require('stdopt')
function lowercase (value) {
if (!(this instanceof lowercase)) {
return new lowercase(value)
}
Opt.call(this, value)
}
lowercase.parse = function (value) {
if (typeof value === 'string' && value.toLowerCase() === value) {
return value.toLowerCase()
}
}
lowercase('oh.').value() // => 'oh.'
lowercase('AHA!').or('oh.').value() // => 'oh.'
lowercase('AHA!').value() // => throws error
Apache-2.0
FAQs
Wrap and validate optional values
The npm package stdopt receives a total of 3,591 weekly downloads. As such, stdopt popularity was classified as popular.
We found that stdopt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.