[Pkg-javascript-commits] [node-simple-is] 01/03: Imported Upstream version 0.2.0+20130421

Julien Puydt julien.puydt at laposte.net
Fri Oct 16 17:28:58 UTC 2015


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-simple-is.

commit cc2933bb8fe63ce9ed8539de201dfd9d8fab3a4b
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Wed Oct 14 21:27:47 2015 +0200

    Imported Upstream version 0.2.0+20130421
---
 LICENSE                 | 19 +++++++++++++++
 README.md               | 50 ++++++++++++++++++++++++++++++++++++++++
 package.json            | 25 ++++++++++++++++++++
 simple-is.js            | 56 +++++++++++++++++++++++++++++++++++++++++++++
 test/simple-is-tests.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 211 insertions(+)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..4b87819
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Olov Lassus <olov.lassus at gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..5c10baf
--- /dev/null
+++ b/README.md
@@ -0,0 +1,50 @@
+# simple-is.js
+A maximally minimal type-testing library. Use it to make your code
+more readable. Works in node and browsers.
+
+
+
+## Usage
+`var is = require("simple-is");`
+
+Use `is.number(x)` instead of `typeof x === "number"` (also `is.boolean`, `is.string`, `is.fn`).
+
+Use `is.nan(x)` instead of `typeof x === "number" && isNaN(x)`, `x !== x` or ES6 `Number.isNaN(x)`.
+
+Use `is.object(x)` instead of `x !== null && typeof x === "object"`.
+
+Use `is.primitive(x)` instead of `x === null || x === undefined || typeof x === "boolean" || typeof x === "number" || typeof x === "string"` (verbose on purpose).
+
+Use `is.array(x)` instead of ES5 `Array.isArray`.
+
+Use `is.finitenumber(x)` instead of `typeof x === "number" && isFinite(x)` or ES6 `Number.isFinite(x)`.
+
+Use `is.someof(x, ["first", 2, obj])` instead of (usually) `x === "first" || x === 2 || x === obj` or (alternatively)  `["first", 2, obj].indexOf(x) >= 0`. Great for reducing copy and paste mistake in `if`-conditions and for making them more readable.
+
+Use `is.noneof(x, ["first", 2, obj])` instead of (usually) `x !== "first" && x !== 2 && x !== obj` or (alternatively)  `["first", 2, obj].indexOf(x) === -1`.
+
+Use `is.own(x, "name")` instead of `Object.prototype.hasOwnProperty.call(x, "name")`.
+
+That's it.
+
+
+
+## Installation
+
+### Node
+Install using npm
+
+    npm install simple-is
+
+```javascript
+var is = require("simple-is");
+```
+
+### Browser
+Clone the repo and include it in a script tag
+
+    git clone https://github.com/olov/simple-is.git
+
+```html
+<script src="simple-is/simple-is.js"></script>
+```
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..15cbbba
--- /dev/null
+++ b/package.json
@@ -0,0 +1,25 @@
+{
+  "name": "simple-is",
+  "version": "0.2.0",
+  "description": "maximally minimal type-testing library",
+  "main": "simple-is.js",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/olov/simple-is.git"
+  },
+  "keywords": [
+    "is",
+    "type",
+    "test",
+    "check",
+    "assert"
+  ],
+  "scripts": {
+    "test": "tap test/*.js"
+  },
+  "devDependencies": {
+    "tap": "~0.4.0"
+  },
+  "author": "Olov Lassus <olov.lassus at gmail.com>",
+  "license": "MIT"
+}
diff --git a/simple-is.js b/simple-is.js
new file mode 100644
index 0000000..a0861dd
--- /dev/null
+++ b/simple-is.js
@@ -0,0 +1,56 @@
+// simple-is.js
+// MIT licensed, see LICENSE file
+// Copyright (c) 2013 Olov Lassus <olov.lassus at gmail.com>
+
+var is = (function() {
+    "use strict";
+
+    var hasOwnProperty = Object.prototype.hasOwnProperty;
+    var toString = Object.prototype.toString;
+    var _undefined = void 0;
+
+    return {
+        nan: function(v) {
+            return v !== v;
+        },
+        boolean: function(v) {
+            return typeof v === "boolean";
+        },
+        number: function(v) {
+            return typeof v === "number";
+        },
+        string: function(v) {
+            return typeof v === "string";
+        },
+        fn: function(v) {
+            return typeof v === "function";
+        },
+        object: function(v) {
+            return v !== null && typeof v === "object";
+        },
+        primitive: function(v) {
+            var t = typeof v;
+            return v === null || v === _undefined ||
+                t === "boolean" || t === "number" || t === "string";
+        },
+        array: Array.isArray || function(v) {
+            return toString.call(v) === "[object Array]";
+        },
+        finitenumber: function(v) {
+            return typeof v === "number" && isFinite(v);
+        },
+        someof: function(v, values) {
+            return values.indexOf(v) >= 0;
+        },
+        noneof: function(v, values) {
+            return values.indexOf(v) === -1;
+        },
+        own: function(obj, prop) {
+            return hasOwnProperty.call(obj, prop);
+        },
+    };
+})();
+
+if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
+    module.exports = is;
+}
diff --git a/test/simple-is-tests.js b/test/simple-is-tests.js
new file mode 100644
index 0000000..ed68fba
--- /dev/null
+++ b/test/simple-is-tests.js
@@ -0,0 +1,61 @@
+"use strict";
+
+var _test = require("tap").test;
+var is = require("../");
+
+function test(name, fn) {
+    return _test(name, function(t) {
+        fn(t, t.ok.bind(t), t.notOk.bind(t));
+    });
+}
+
+test("various", function(t, yes, no) {
+    function fn() {
+    }
+
+    yes(is.nan(NaN));
+    no(is.nan(1));
+    no(is.nan("asdf"));
+
+    yes(is.boolean(true));
+    no(is.boolean(new Boolean(true)));
+
+    yes(is.number(1));
+    no(is.number(new Number(1)));
+
+    yes(is.string("asdf"));
+    no(is.string(new String("asdf")));
+
+    yes(is.fn(fn));
+    no(is.fn({}));
+
+    yes(is.object({}));
+    no(is.object(null));
+    no(is.object(fn));
+
+    yes([null, undefined, true, 1, "asdf"].every(is.primitive));
+    no([{}, fn, new Number(1), /regexp/].some(is.primitive));
+
+    yes(is.array([]));
+
+    yes(is.finitenumber(1));
+    yes(is.finitenumber(1.1));
+    no(is.finitenumber(NaN));
+    no(is.finitenumber(Infinity));
+    no(is.finitenumber(-Infinity));
+    no(is.finitenumber("1"));
+
+    yes(is.someof(1, [-1, 1, 3]));
+    yes(is.someof("x", [0, 1, "x"]));
+    no(is.someof(1, ["1"]));
+
+    no(is.noneof(1, [-1, 1, 3]));
+    no(is.noneof("x", [0, 1, "x"]));
+    yes(is.noneof(1, ["1"]));
+
+    yes(is.own({a: 1}, "a"));
+    no(is.own({a: 1}, "b"));
+    no(is.own({a: 1}, "toString"));
+
+    t.end();
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-simple-is.git



More information about the Pkg-javascript-commits mailing list