vigour-util
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -15,3 +15,3 @@ 'use strict' | ||
let type = typeof definition | ||
if (type === 'function' || type !== 'object' || type._base_version) { | ||
if (type === 'function' || type !== 'object' || val[key]._base_version) { | ||
definition = { value: definition } | ||
@@ -18,0 +18,0 @@ } |
'use strict' | ||
var email = /^([^.](?![a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~]+\.\.)([a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~.]+[^.])|([a-zA-Z0-9]))@([A-Za-z0-9-]{1,64}\.){1,10}[a-zA-Z]{2,64}$/ | ||
var email = /^([^.](?![a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~]+\.\.)([a-zA-Z0-9!#$%&'*+\-/=?^_`{|}~.]+[^.])|([a-zA-Z0-9]{1,2}))@([A-Za-z0-9-]{1,64}\.){1,10}[a-zA-Z]{2,64}$/ | ||
@@ -7,3 +7,3 @@ /** | ||
* Checks whether provided parameter looks like a valid e-mail address | ||
* @param {string} val - the string to process | ||
* @param {string} val - the string to check | ||
* @returns {boolean} `true` if `val` is a valid e-mail address, `false` otherwise | ||
@@ -10,0 +10,0 @@ */ |
'use strict' | ||
var isHash = /^[a-z\d]{5,7}$/ | ||
var isNumber = require('./number') | ||
/** | ||
* @function isHash | ||
* Checks is a string looks like a hash generated by the [`hash`](#hash) utility | ||
* @param {string} val - the string to check | ||
* @returns {boolean} `true` if `val` looks like a hash, `false` otherwise | ||
*/ | ||
module.exports = (val) => !isNumber(val) && isHash.test(val) |
'use strict' | ||
/** | ||
* `true` if in node context, `false` otherwise | ||
*/ | ||
module.exports = typeof window === 'undefined' | ||
// || window.toString() === '[object global]' |
'use strict' | ||
var isNumber = require('./number') | ||
/** | ||
* @function isNumberLike | ||
* Checks whether provided parameter looks like a number | ||
* @param {any} val - the value to check | ||
* @returns {boolean} `true` if `val` looks like a number, `false` otherwise | ||
*/ | ||
module.exports = function isNumberLike (val) { | ||
@@ -5,0 +11,0 @@ if (val === null || val === void 0 || val === false) { |
'use strict' | ||
var isStream = require('./stream') | ||
/** | ||
* @function isPlainObj | ||
* Checks whether an object is a plain object | ||
* @param {object} obj - the object to check | ||
* @returns {boolean} `true` if `obj` is a plain object, `false` otherwise | ||
*/ | ||
module.exports = function isPlainObj (obj) { | ||
@@ -5,0 +11,0 @@ return ( |
'use strict' | ||
/** | ||
* @function isRemoved | ||
* Checks if a property has been removed | ||
* @param {Base} base - the property to check | ||
* @returns {boolean} `true` if `base` has been removed, `false` otherwise | ||
*/ | ||
module.exports = function isRemoved (base) { | ||
@@ -3,0 +9,0 @@ if (base === null) { |
@@ -7,2 +7,8 @@ 'use strict' | ||
/** | ||
* @function isStream | ||
* Checks whether provided argument is a stream | ||
* @param {object} val - the object to check | ||
* @returns {boolean} `true` if `val` is a stream, `false` otherwise | ||
*/ | ||
module.exports = exports = function (val) { | ||
@@ -15,2 +21,8 @@ return val && typeof val === 'object' && ( | ||
/** | ||
* @function isStream.readable | ||
* Checks whether provided argument is a readable stream | ||
* @param {object} val - the object to check | ||
* @returns {boolean} `true` if `val` is a readable stream, `false` otherwise | ||
*/ | ||
exports.readable = function (val) { | ||
@@ -28,2 +40,8 @@ return val && ( | ||
/** | ||
* @function isStream.writable | ||
* Checks whether provided argument is a writable stream | ||
* @param {object} val - the object to check | ||
* @returns {boolean} `true` if `val` is a writable stream, `false` otherwise | ||
*/ | ||
exports.writable = function (val) { | ||
@@ -30,0 +48,0 @@ return val && ( |
'use strict' | ||
var isNode = require('./node') | ||
/** | ||
* @function isTouch | ||
* Checks if we're running in a touch-enabled context | ||
* @returns {boolean} `true` if we're in a touch-enabled context, `false` otherwise | ||
*/ | ||
module.exports = isNode | ||
@@ -4,0 +9,0 @@ ? false |
'use strict' | ||
var url = /^(((ws(s)?)|(http(s)?))\:\/\/)?[a-zA-Z0-9_-]+(\.|\:)([^\/\/])[a-zA-Z/0-9$-/:-?{#-~!"^_`\[\]]+$/ | ||
/** | ||
* @function isUrl | ||
* Checks if a string is a valid url | ||
* @param {string} val - the string to check | ||
* @returns {boolean} `true` if `val` is a valid url, `false` otherwise | ||
*/ | ||
module.exports = function (val) { | ||
return typeof val === 'string' && url.test(val) || val === 'localhost' | ||
} |
{ | ||
"name": "vigour-util", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"author": "Vigour.io <dev@vigour.io>", | ||
"scripts": { | ||
"test": "node test" | ||
"test": "node test | tap-difflet" | ||
}, | ||
@@ -40,4 +40,5 @@ "repository": { | ||
"tape": "^4.5.1", | ||
"vigour-base": "^1.2.2" | ||
"vigour-base": "^1.2.2", | ||
"tap-difflet": "0.4.0" | ||
} | ||
} |
'use strict' | ||
/** | ||
* @function pathContains | ||
* Checks whether a key is part of an array, allowing for prefixed keys | ||
* @param {array} path - the array to look in | ||
* @param {string} key - the key to check for | ||
* @returns {boolean} `true` if `key` is found in `array`, `false` otherwise | ||
*/ | ||
module.exports = function pathContains (path, key) { | ||
@@ -3,0 +10,0 @@ for (var i = 0, len = path.length; i < len; i++) { |
188
README.md
[![Build Status](https://travis-ci.org/vigour-io/util.svg?branch=master)](https://travis-ci.org/vigour-io/util) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
[![npm version](https://badge.fury.io/js/vigour-util.svg)](https://badge.fury.io/js/vigour-util) | ||
@@ -13,50 +15,37 @@ # vigour-util | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
### Contents | ||
--- | ||
- [isEmail](#isemail) | ||
- [isHash](#ishash) | ||
- [isNode](#isnode) | ||
- [isNumber](#isnumber) | ||
- [isNumberlike](#isnumberlike) | ||
- [isStream](#isstream) | ||
- [isStream.readable](#isstreamreadable) | ||
- [isStream.writable](#isstreamwritable) | ||
- [isTouch](#istouch) | ||
- [isUrl](#isurl) | ||
- [isPlainobj](#isplainobj) | ||
- [isRemoved](#isremoved) | ||
- [isEmpty](#isempty) | ||
- [pathContains](#pathcontains) | ||
- [uuid.val](#uuidval) | ||
- [uuid.generate](#uuidgenerate) | ||
- [define](#define) | ||
- [descriptors](#descriptors) | ||
- [flatten](#flatten) | ||
- [hash](#hash) | ||
- [include](#include) | ||
- [merge](#merge) | ||
- [regenerator](#regenerator) | ||
- [setwithpath](#setwithpath) | ||
- [unflatten](#unflatten) | ||
- [getReference](#getreference) | ||
- [get](#get) | ||
- [reference](#reference) | ||
- [is](#is) | ||
- [email](#email) | ||
- [empty](#empty) | ||
- [hash](#hash) | ||
- [node](#node) | ||
- [number](#number) | ||
- [numberlike](#numberlike) | ||
- [plainobj](#plainobj) | ||
- [removed](#removed) | ||
- [stream](#stream) | ||
- [touch](#touch) | ||
- [url](#url) | ||
- [path](#path) | ||
- [contains](#contains) | ||
- [Others](#others) | ||
- [define](#define) | ||
- [descriptors](#descriptors) | ||
- [flatten](#flatten) | ||
- [hash](#hash-1) | ||
- [include](#include) | ||
- [merge](#merge) | ||
- [regenerator](#regenerator) | ||
- [setwithpath](#setwithpath) | ||
- [unflatten](#unflatten) | ||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
## get | ||
--- | ||
### [reference](get/reference.js) | ||
(*Specific to `vigour-base`*) Get's the referenced object | ||
```javascript | ||
var Base = require('vigour-base') | ||
var getReference = require('../../get/reference') | ||
var a = new Base({}) | ||
var b = new Base(a) | ||
getReference(b) // a | ||
``` | ||
## is | ||
### [email](is/email.js) | ||
### [isEmail](is/email.js) | ||
Checks whether provided parameter looks like a valid e-mail address | ||
@@ -69,12 +58,3 @@ ```javascript | ||
### [empty](is/empty.js) | ||
(*Specific to `vigour-base`*) Checks if a `Base` object is empty | ||
```javascript | ||
var isEmpty = require('vigour-util/is/empty') | ||
var Base = require('vigour-base') | ||
isEmpty(new Base({})) // true | ||
isEmpty(new Base({ awesome: true })) // false | ||
``` | ||
### [hash](is/hash.js) | ||
### [isHash](is/hash.js) | ||
Checks is a string looks like a hash generated by the [`hash`](#hash) utility | ||
@@ -86,3 +66,3 @@ ```javascript | ||
### [node](is/node.js) | ||
### [isNode](is/node.js) | ||
Check whether we're running in `node` | ||
@@ -94,4 +74,4 @@ ```javascript | ||
### [number](is/number.js) | ||
Checks whether provided parameter is a number | ||
### [isNumber](is/number.js) | ||
Alias for [`lodash.isfinite`](https://www.npmjs.com/package/lodash.isfinite) | ||
```javascript | ||
@@ -103,3 +83,3 @@ var isNumber = require('vigour-util/is/number') | ||
### [numberlike](is/numberlike.js) | ||
### [isNumberlike](is/numberlike.js) | ||
Checks whether provided parameter looks like a number | ||
@@ -112,22 +92,22 @@ ```javascript | ||
### [plainobj](is/plainobj.js) | ||
Checks whether an object is a plain object. (*Compatible with `vigour-base`*) | ||
### [isStream](is/stream.js) | ||
Checks whether provided argument implements the stream interface | ||
```javascript | ||
var isPlainObj = require('vigour/util/is/plainobj') | ||
isPlainObj({}) // true | ||
isPlainObj(new Base({})) // false | ||
var stream = require('stream') | ||
var rs = new stream.Readable() | ||
var isStream = require('vigour-util/is/stream') | ||
isStream(rs) // true | ||
``` | ||
### [removed](is/removed.js) | ||
(*Specific to `vigour-base`*) Checks if a property has been removed | ||
#### [isStream.readable](is/stream.js) | ||
Checks whether provided argument implements the readable stream interface | ||
```javascript | ||
var isRemoved = require('vigour-util/is/removed') | ||
var Base = require('vigour-base') | ||
var base = new Base({ bad: true }) | ||
base.bad.remove() | ||
isRemoved(base.bad) // true | ||
var stream = require('stream') | ||
var rs = new stream.Readable() | ||
var isStream = require('vigour-util/is/stream') | ||
isStream.readable(rs) // true | ||
``` | ||
### [stream](is/stream.js) | ||
Checks whether provided argument is a stream | ||
#### [isStream.writable](is/stream.js) | ||
Checks whether provided argument implements the writable stream interface | ||
```javascript | ||
@@ -137,7 +117,7 @@ var stream = require('stream') | ||
var isStream = require('vigour-util/is/stream') | ||
isStream(rs) // true | ||
isStream.writable(rs) // false | ||
``` | ||
### [touch](is/touch.js) | ||
Checks if we're running in a touch-enabled context. | ||
### [isTouch](is/touch.js) | ||
Checks if we're running in a touch-enabled context | ||
```javascript | ||
@@ -148,4 +128,4 @@ var isTouch = require('vigour-util/is/touch') | ||
### [url](is/url.js) | ||
Checks if a string is a valid `url` | ||
### [isUrl](is/url.js) | ||
Checks if a string is a valid url | ||
```javascript | ||
@@ -157,5 +137,31 @@ var isUrl = require('vigour-util/is/url') | ||
## path | ||
### [contains](path/contains.js) | ||
### [isPlainobj](is/plainobj.js) | ||
Checks whether an object is a plain object. (*Compatible with `vigour-base`*) | ||
```javascript | ||
var isPlainObj = require('vigour/util/is/plainobj') | ||
isPlainObj({}) // true | ||
isPlainObj(new Base({})) // false | ||
``` | ||
### [isRemoved](is/removed.js) | ||
(*Specific to `vigour-base`*) Checks if a property has been removed | ||
```javascript | ||
var isRemoved = require('vigour-util/is/removed') | ||
var Base = require('vigour-base') | ||
var base = new Base({ bad: true }) | ||
base.bad.remove() | ||
isRemoved(base.bad) // true | ||
``` | ||
### [isEmpty](is/empty.js) | ||
(*Specific to `vigour-base`*) Checks if a `Base` object is empty | ||
```javascript | ||
var isEmpty = require('vigour-util/is/empty') | ||
var Base = require('vigour-base') | ||
isEmpty(new Base({})) // true | ||
isEmpty(new Base({ awesome: true })) // false | ||
``` | ||
### [pathContains](path/contains.js) | ||
Checks whether a key is part of an array, allowing for prefixed keys | ||
@@ -167,4 +173,16 @@ ```javascript | ||
## Others | ||
### [uuid.val](uuid/index.js) | ||
A process-specific unique ID (or device-specific on browser) | ||
```javascript | ||
var uuid = require('vigour-util/uuid') | ||
uuid.val // 'oj0beu' | ||
``` | ||
#### [uuid.generate](uuid/index.js) | ||
Generates a process-specific unique ID (or device-specific on browser) | ||
```javascript | ||
var uuid = require('vigour-util/uuid') | ||
uuid.generate() // '14hdmru' | ||
``` | ||
### [define](define.js) | ||
@@ -259,1 +277,13 @@ Defines new or modifies existing properties (using [`Object.defineProperty`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)) on an object, setting `configurable: true` by default. | ||
``` | ||
### [getReference](get/reference.js) | ||
(*Specific to `vigour-base`*) Get's the referenced object | ||
```javascript | ||
var Base = require('vigour-base') | ||
var getReference = require('../../get/reference') | ||
var a = new Base({}) | ||
var b = new Base(a) | ||
getReference(b) // a | ||
``` |
@@ -8,14 +8,22 @@ 'use strict' | ||
test('define', function (t) { | ||
t.plan(5) | ||
t.plan(6) | ||
var subject = {} | ||
var value = 'v' | ||
var fn = function () {} | ||
var base = new Base() | ||
var set = function (val) { | ||
t.equal(val, value, 'value is the same') | ||
} | ||
var extras = [ | ||
{ one: true, two: new Base() }, | ||
{ three: function () {} }, | ||
{ one: true }, | ||
{ three: fn }, | ||
{ | ||
four: { | ||
get () { }, | ||
set (val) { t.equal(val, value, 'value is the same') } | ||
get: fn, | ||
set: set | ||
} | ||
}, | ||
{ | ||
a: base, | ||
b: fn | ||
} | ||
@@ -25,8 +33,31 @@ ] | ||
define.apply(subject, extras) | ||
t.equals(Object.getOwnPropertyDescriptor(subject, 'one').configurable, true) | ||
t.equals(Object.getOwnPropertyDescriptor(subject, 'two').configurable, true) | ||
t.equals(Object.getOwnPropertyDescriptor(subject, 'three').configurable, true) | ||
t.equals(Object.getOwnPropertyDescriptor(subject, 'four').configurable, true) | ||
equal('two', void 0) | ||
equal('three', { value: fn }) | ||
equal('four', { get: fn, set: set }) | ||
equal('a', { value: base }) | ||
equal('b', { value: fn }) | ||
// Let's trigger the setter test | ||
subject.four = value | ||
function equal (field, val) { | ||
var descriptor | ||
if (val !== void 0) { | ||
descriptor = { | ||
configurable: true, | ||
enumerable: false | ||
} | ||
if (val.value && !val.hasOwnProperty('writable')) { | ||
descriptor.writable = false | ||
} | ||
if (val) { | ||
for (let i in val) { | ||
descriptor[i] = val[i] | ||
} | ||
} | ||
} | ||
t.deepLooseEqual( | ||
Object.getOwnPropertyDescriptor(subject, field), descriptor, | ||
'"' + field + '" is defined correctly' | ||
) | ||
} | ||
}) |
@@ -10,2 +10,7 @@ 'use strict' | ||
Super.prototype.a = 'a' | ||
Object.defineProperty(Super.prototype, 'c', { | ||
writable: false, | ||
configurable: false | ||
}) | ||
function Sub () {} | ||
@@ -27,3 +32,6 @@ Sub.prototype = Super.prototype | ||
[instance, | ||
{ a: { configurable: true, enumerable: true, value: 'a', writable: true } } | ||
{ | ||
a: { configurable: true, enumerable: true, value: 'a', writable: true }, | ||
c: { configurable: false, enumerable: false, value: void 0, writable: false } | ||
} | ||
] | ||
@@ -30,0 +38,0 @@ ] |
@@ -27,2 +27,3 @@ 'use strict' | ||
['💩', false], | ||
['aa', true], | ||
// Let's mix things up | ||
@@ -84,3 +85,3 @@ ['"very.unusual.@.unusual.com"@example.com', false], | ||
if (msg.length > max) { | ||
return msg.slice(0, max) + '...' | ||
return msg.slice(0, max) + '[TRUNCATED]' | ||
} else { | ||
@@ -87,0 +88,0 @@ return msg |
'use strict' | ||
var hash = require('../hash') | ||
var rand = ~~(Math.random() * 10000) | ||
var stamp = Date.now() | ||
exports.val = hash( | ||
'b-' + | ||
stamp + | ||
'-' + | ||
rand + | ||
'-' + window.navigator.userAgent + | ||
'-' + window.navigator.userLanguage + | ||
window.navigator.language | ||
) | ||
/** | ||
* @function uuid.generate | ||
* Generates a unique ID | ||
* @returns {string} A unique ID | ||
*/ | ||
exports.generate = function () { | ||
var rand = ~~(Math.random() * 10000) | ||
var stamp = Date.now() | ||
return hash( | ||
'b-' + | ||
stamp + | ||
'-' + | ||
rand + | ||
'-' + window.navigator.userAgent + | ||
'-' + window.navigator.userLanguage + | ||
window.navigator.language | ||
) | ||
} | ||
/** | ||
* exports.val is a unique ID | ||
*/ | ||
exports.val = exports.generate() |
'use strict' | ||
var hash = require('../hash') | ||
var rand = ~~(Math.random() * 10000) | ||
var stamp = Date.now() | ||
/** | ||
* @function uuid.generate | ||
* Generates a unique ID | ||
* @returns {string} A unique ID | ||
*/ | ||
exports.generate = function () { | ||
var rand = ~~(Math.random() * 10000) | ||
var stamp = Date.now() | ||
return hash('n-' + process.pid + '-' + stamp + '-' + rand) | ||
} | ||
/** | ||
* exports.val is a unique ID | ||
*/ | ||
exports.val = exports.generate() |
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
59990
1559
279
3