+37
| isdefined | ||
| ========= | ||
| There is a nice feature of JSON.stringify that removes undefined object properties which works as follows: | ||
| ````javascript | ||
| var obj: { | ||
| a: 'test', | ||
| b: null, | ||
| c: undefined | ||
| } | ||
| console.log(JSON.stringify(obj)) // '{"a":"test","b":null}' | ||
| ```` | ||
| This makes for handy formatting of JSON when responding to requests. This module provides some utility methods for working with undefined object properties, so if you're sick of writing or seeing `typeof x !== undefined` multiple times in your codebase when using the above method to format JSON, this module comes in handy. | ||
| Usage: | ||
| ````javascript | ||
| var defined = require('isdefined') | ||
| var x = { | ||
| a: true | ||
| } | ||
| console.log(defined.is_defined(x.a)) // true | ||
| console.log(defined.is_defined(x.b)) // false | ||
| console.log(defined.boolean_to_binary(x.a)) // 1 /* handy for when 1 or 0 is used for storing true/false */ | ||
| console.log(defined.boolean_to_binary(x.b)) // undefined | ||
| // useful for the likes of this statement: | ||
| var test = typeof x['a'] !== 'undefined' ? (i['active'] == 1) : undefined | ||
| // converts to | ||
| var test = defined.binary_to_boolean(x['a']) | ||
| ```` |
+40
-1
@@ -1,1 +0,40 @@ | ||
| module.exports = function(v) { return typeof v !== 'undefined' }; | ||
| var defined; | ||
| /** | ||
| * Return whether variable is defined | ||
| * @param v | ||
| * @returns {boolean} | ||
| */ | ||
| defined.is_defined = function (v) { | ||
| return typeof v !== 'undefined'; | ||
| } | ||
| /** | ||
| * If variable defined, return whether equals 1 | ||
| * Else return undefined | ||
| * @param v | ||
| * @returns {boolean} | ||
| */ | ||
| defined.binary_to_boolean = function(v) { | ||
| return defined.is_defined(v) ? v == 1 : undefined; | ||
| }; | ||
| /** | ||
| * If variable defined, return binary value of boolean | ||
| * Else return undefined | ||
| * @param v | ||
| * @returns {*} | ||
| */ | ||
| defined.boolean_to_binary = function(v) { | ||
| if(defined.is_defined(v)){ | ||
| if(v == 'true' || v == true) { | ||
| return 1; | ||
| } else { | ||
| return 0; | ||
| } | ||
| } else { | ||
| return undefined; | ||
| } | ||
| }; | ||
| module.exports = defined; |
+1
-1
| { | ||
| "name": "isdefined", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "Checks a given object property is defined", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
2432
287.88%3
50%35
Infinity%1
-50%38
Infinity%0
-100%