Skip to content

Commit

Permalink
Refactoring of the library
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Schmidt committed Oct 9, 2018
1 parent c3df791 commit 4733aa7
Show file tree
Hide file tree
Showing 10 changed files with 1,863 additions and 67 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
90 changes: 34 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,69 @@
# Isset Helper

**A tiny helper method for Javascript, checking a variable for existence.**

Since checking variable types and existence always has been painful in Javascript, validation conditions normally are exhausting.
This module solves this definitely.
This module solves this definitely.

---

## Installation

```
### as a module

Install the library through your preferred package manager:

``` bash
$ npm install --save isset-helper
# or
$ yarn add isset-helper
```

## Require
Then you can require the module as usual:

```JavaScript
``` JavaScript
const isset = require('isset-helper');
```

or if you prefer a more object-oriented usage of this method:
### in the browser

Include _one_ of the following script tags in your pages head:

```JavaScript
const Util = {
isset: require('isset-helper')
}
``` 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/dist/isset.min.js">
<script src="https://unpkg.com/isset-helper@2/dist/isset.min.js">
```
The library registers the `isset` function automatically on the `window` object.
## Usage
```JavaScript
isset(variable,type) {}
// or
Util.isset(variable,type) {}
``` JavaScript
isset(variable, type);
```
`variable` can be anything. It's value is going to be checked.
`type` can be a string, class or even left empty.
`variable` can be anything. It's value is going to be checked.
`type` can be a string, class or even left out.
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
**Please notice:**
- The algorithm always runs the `instanceof`-check, leaving it out of consideration if the check runs in any error.
**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.
## Examples
### Form validation
_(Example below use jQuery syntax for simplification. It's not required for the helper to work!)_

```JavaScript
$('#myform').on('submit',function (event) {
var user = $(this).find('#username').val()
var pass = $(this).find('#password').val()

return (Util.isset(user,'string') && Util.isset(pass,'string'))
})
```
## Contributing
### Constructor validation
```JavaScript
class House {...}

class Person {
constructor(name,age,house) {
if(Util.isset(name,'string')) {
this.name = name;
} else {
throw new Error('Person constructor is missing required argument: name')
}

if(Util.isset(age,'number')) {
this.age = age;
} else {
throw new Error('Person constructor is missing required argument: age')
}

if(Util.isset(house,House)) {
this.house = house;
} else {
throw new Error('Person constructor is missing required argument: house')
}
}
}
```
If, contrary to expectations, you find an error in the function, please report it to the Issues page.
## License
This library is licensed under the MIT License.
You may find [a copy of the license](/blob/master/LICENSE) at the root of the project source.
1 change: 1 addition & 0 deletions dist/isset.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const gulp = require('gulp'),
pump = require('pump'),
header = require('gulp-header'),
plumber = require('gulp-plumber'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename');

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);
});

gulp.task('build-web', (finished) => {
pump([
gulp.src('./src/main.js'),
plumber(),
header('window.isset = '),
uglify(),
rename('isset.min.js'),
gulp.dest('./dist/')
], finished);
});

gulp.task('build', ['build-module', 'build-web']);

gulp.task('default', ['build'], () => {
gulp.watch('./src/main.js', ['build']);
});
30 changes: 20 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
{
"name": "isset-helper",
"version": "1.1.1",
"version": "2.0.0",
"description": "A tiny helper method, checking a variable for existence",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"main": "./dist/module.js",
"author": {
"name": "Maximilian Schmidt",
"email": "[email protected]",
"url": "https://www.mcstreetguy.de/"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/MCStreetguy/isset-helper/issues",
"email": "[email protected]"
},
"homepage": "https://github.com/MCStreetguy/isset-helper#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/MCStreetguy/isset-helper.git"
},
"author": "Maximilian Schmidt <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/MCStreetguy/isset-helper/issues"
},
"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",
"pump": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion main.js → src/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function isset(test,type) {
function isset(test,type) {
var _instance, _check;

if(!type) {
Expand Down
Loading

0 comments on commit 4733aa7

Please sign in to comment.