isset-helper
Advanced tools
Comparing version 2.0.2 to 3.0.0
@@ -1,1 +0,1 @@ | ||
window.isset=function(t,n){var e,r;if(n){r=!0;try{e=t instanceof n}catch(t){e=!1;try{n=n.toLowerCase()}catch(t){r=!1}}}else r=!1;return null!=t&&null!=t&&(!r||typeof t===n||e)&&(!r||"string"!=n||""!=t&&" "!=t)}; | ||
"use strict";window.isset=function isset(t,r){var i,n;if(r){n=!0;try{i=t instanceof r}catch(t){i=!1,n="string"==typeof r}}else n=!1;return null!=t&&null!=t&&(!n||(typeof t===r||i||"array"===r&&Array.isArray(t))&&("string"!=r||"string"==typeof t&&t.trim().length>0))}; |
@@ -1,36 +0,50 @@ | ||
const gulp = require('gulp'), | ||
pump = require('pump'), | ||
header = require('gulp-header'), | ||
plumber = require('gulp-plumber'), | ||
uglify = require('gulp-uglify'), | ||
rename = require('gulp-rename'); | ||
const gulp = require('gulp'); | ||
const pump = require('pump'); | ||
const replace = require('gulp-replace'); | ||
const rename = require('gulp-rename'); | ||
const terser = require('gulp-terser'); | ||
const umd = require('gulp-umd'); | ||
gulp.task('build-module', (finished) => { | ||
pump([ | ||
gulp.src('./src/main.js'), | ||
plumber(), | ||
header('module.exports = '), | ||
uglify({ | ||
keep_fnames: true | ||
}), | ||
rename('module.js'), | ||
gulp.dest('./dist/') | ||
], finished); | ||
}); | ||
exports['build-universal-module'] = (done) => { | ||
pump([ | ||
gulp.src('./src/main.js'), | ||
umd({ | ||
templateName: 'amdNodeWeb', | ||
exports: (file) => 'isset', | ||
namespace: (file) => 'isset', | ||
}), | ||
terser({ | ||
keep_fnames: /^isset$/ | ||
}), | ||
rename('module.umd.js'), | ||
gulp.dest('./dist/') | ||
], done); | ||
} | ||
gulp.task('build-web', (finished) => { | ||
pump([ | ||
gulp.src('./src/main.js'), | ||
plumber(), | ||
header('window.isset = '), | ||
uglify(), | ||
rename('isset.min.js'), | ||
gulp.dest('./dist/') | ||
], finished); | ||
}); | ||
exports['build-commonjs-module'] = (done) => { | ||
pump([ | ||
gulp.src('./src/main.js'), | ||
replace('function', 'module.exports = function'), | ||
terser({ | ||
keep_fnames: /^isset$/ | ||
}), | ||
rename('module.cjs'), | ||
gulp.dest('./dist/') | ||
], done); | ||
} | ||
gulp.task('build', ['build-module', 'build-web']); | ||
exports['build-for-web'] = (done) => { | ||
pump([ | ||
gulp.src('./src/main.js'), | ||
replace('function', 'window.isset = function'), | ||
terser({ | ||
keep_fnames: /^isset$/ | ||
}), | ||
rename('isset.min.js'), | ||
gulp.dest('./dist/') | ||
], done); | ||
} | ||
gulp.task('default', ['build'], () => { | ||
gulp.watch('./src/main.js', ['build']); | ||
}); | ||
exports['build'] = gulp.parallel(exports['build-universal-module'], exports['build-commonjs-module'], exports['build-for-web']); | ||
exports['watch'] = () => gulp.watch('./src/main.js', exports['build']); | ||
exports['default'] = gulp.series(exports['build'], exports['watch']); |
{ | ||
"name": "isset-helper", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "A tiny helper method, checking a variable for existence", | ||
"main": "./dist/module.js", | ||
"main": "./dist/module.umd.js", | ||
"author": { | ||
@@ -13,4 +13,3 @@ "name": "Maximilian Schmidt", | ||
"bugs": { | ||
"url": "https://github.com/MCStreetguy/isset-helper/issues", | ||
"email": "maximilianschmidt404@gmail.com" | ||
"url": "https://github.com/MCStreetguy/isset-helper/issues" | ||
}, | ||
@@ -23,9 +22,9 @@ "homepage": "https://github.com/MCStreetguy/isset-helper#readme", | ||
"devDependencies": { | ||
"gulp": "^3.9.1", | ||
"gulp-header": "^2.0.5", | ||
"gulp-plumber": "^1.2.0", | ||
"gulp-rename": "^1.4.0", | ||
"gulp-uglify": "^3.0.1", | ||
"gulp-rename": "^2.0.0", | ||
"gulp-replace": "^1.1.3", | ||
"gulp-terser": "^2.0.1", | ||
"gulp-umd": "^2.0.0", | ||
"gulp": "^4.0.2", | ||
"pump": "^3.0.0" | ||
} | ||
} |
# Isset Helper | ||
**A tiny helper method for Javascript, checking a variable for existence.** | ||
**Get rid of weak typing and improve your assertions: A tiny helper method for Javascript** | ||
Since checking variable types and existence always has been painful in Javascript, validation conditions normally are exhausting. | ||
Since checking variable types and existence always has been painful in Javascript, validation assertions normally are exhausting to write. | ||
This module solves this definitely. | ||
@@ -16,3 +16,3 @@ | ||
``` bash | ||
```bash | ||
$ npm install --save isset-helper | ||
@@ -25,3 +25,3 @@ # or | ||
``` JavaScript | ||
```js | ||
const isset = require('isset-helper'); | ||
@@ -34,7 +34,7 @@ ``` | ||
``` html | ||
<script src="https://cdn.jsdelivr.net/npm/isset-helper@2/dist/isset.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/gh/mcstreetguy/isset-helper@2/dist/isset.min.js"></script> | ||
<script src="https://bundle.run/isset-helper@2.0/dist/isset.min.js"></script> | ||
<script src="https://unpkg.com/isset-helper@2/dist/isset.min.js"></script> | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/isset-helper@3/dist/isset.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/gh/mcstreetguy/isset-helper@3/dist/isset.min.js"></script> | ||
<script src="https://bundle.run/isset-helper@3.0/dist/isset.min.js"></script> | ||
<script src="https://unpkg.com/isset-helper@3/dist/isset.min.js"></script> | ||
``` | ||
@@ -46,19 +46,19 @@ | ||
``` JavaScript | ||
```js | ||
isset(variable, type); | ||
``` | ||
`variable` can be anything. It's value is going to be checked. | ||
`type` can be a string, class or even left out. | ||
`variable` can be anything, it's value is going to be checked. | ||
`type` can be a string, object constructor or even be omitted. | ||
The algorithm follows the subsequent rules: | ||
The algorithm follows the subsequent rules: | ||
1. `variable` is not `null` | ||
2. `variable` is not `undefined` | ||
3. If `type` is a string, `typeof variable` has to match `type` | ||
4. Otherwise, `variable` has to match `instanceof type` | ||
5. If `type` is `"string"`, `variable` is not an empty string | ||
3. If `type` is a string, `typeof variable` has to match `type`, otherwise, `variable` has to match `instanceof type` | ||
4. If `type` is `"string"`, the length of trimmed `variable` is greater than zero | ||
5. If `type` is `"array"`, `variable` has to pass the [`Array.isArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) check | ||
**Please notice:** | ||
**Please notice:** | ||
- The algorithm always runs the `instanceof`-check, leaving it out of consideration if the check produces any error. | ||
- The algorithm doesn't check for the exact value (apart from the empty string case mentioned above), thus `false` will also be considered valid. | ||
@@ -69,2 +69,3 @@ | ||
If, contrary to expectations, you find an error in the function, please report it to the Issues page. | ||
Feel free to make changes to a fork yourself and propose a detailed pull request when finished. | ||
@@ -71,0 +72,0 @@ ## License |
@@ -1,5 +0,24 @@ | ||
function isset(test,type) { | ||
var _instance, _check; | ||
'use strict'; | ||
if(!type) { | ||
/** | ||
* Check a variable for existence and validate it's type easily. | ||
* | ||
* @param {*} test The value/variable to test | ||
* @param {string|function} type The expected type or an object constructor to test for | ||
* @returns {boolean} true if all checks succeeded, false otherwise | ||
*/ | ||
function isset(test, type) { | ||
/** | ||
* @desc true if the instanceof check succeeded, false otherwise | ||
* @type {boolean} | ||
*/ | ||
var _instance; | ||
/** | ||
* @desc false if no type check should be executed at all, true otherwise | ||
* @type {boolean} | ||
*/ | ||
var _check; | ||
if (!type) { | ||
_check = false; | ||
@@ -13,8 +32,3 @@ } else { | ||
_instance = false; | ||
try { | ||
type = type.toLowerCase(); | ||
} catch(e) { | ||
_check = false; | ||
} | ||
_check = (typeof type === 'string') | ||
} | ||
@@ -26,7 +40,15 @@ } | ||
test != undefined && ( | ||
!_check || (typeof test === type || _instance) | ||
) && ( | ||
!_check || (type != 'string') || (test != '' && test != ' ') | ||
// skip all the typechecks entirely if not desired | ||
!_check || ( | ||
// evaluates to true if the typecheck succeeded or the previous instanceof check succeeded | ||
(typeof test === type || _instance) || ( | ||
// special edge-case check for type 'array' as these are technically of type 'object' | ||
type === 'array' && Array.isArray(test) | ||
) | ||
) && ( | ||
// evaluates to true if the requested type is not a string or the value is not an empty string | ||
(type != 'string') || (typeof test === 'string' && test.trim().length > 0) | ||
) | ||
) | ||
); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8626
11
92
71