type-inspect
Advanced tools
Comparing version
{ | ||
"name": "type-inspect", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Type Inspector", | ||
@@ -30,3 +30,3 @@ "main": "src/app.js", | ||
"lagoon-reporter": "^0.7.9", | ||
"mocha": "^9.2.1" | ||
"mocha": "^9.2.2" | ||
}, | ||
@@ -33,0 +33,0 @@ "dependencies": { |
@@ -155,1 +155,47 @@ TypeInspect | ||
Parse a diff result and returns value as string. | ||
### Matcher | ||
With version `v2.2.0` matcher support was introduced. A matcher allows to match a value by it's data type, it can be placed in the right side of a diff call. | ||
```js | ||
const {Matcher, TypeInspect} = require('type-inspect') | ||
const left = { | ||
name: 'Banana', | ||
isFruit: true | ||
} | ||
const matcher = new Matcher() | ||
const right = { | ||
name: matcher.isString(), | ||
isFruit: matcher.isBoolean() | ||
} | ||
const diff = TypeInspect.diff(left, right) | ||
if (diff.isDifferent === false) { | ||
console.log('Left and right is identical') | ||
} | ||
``` | ||
##### Supported matchers: | ||
| Name | Description | | ||
| --------------- | ----------------------------- | | ||
| `isArray()` | Value is a Array | | ||
| `isAsync()` | Value is a Async function | | ||
| `isBoolean()` | Value is a Boolean | | ||
| `isClass()` | Value is a Class | | ||
| `isDate()` | Value is a Date | | ||
| `isFunction()` | Value is a Function | | ||
| `isGenerator()` | Value is a Generator function | | ||
| `isMap()` | Value is a Map | | ||
| `isNaN()` | Value is `NaN` | | ||
| `isNull()` | Value is `null` object | | ||
| `isNumber()` | Value is a Number | | ||
| `isObject()` | Value is an Object | | ||
| `isPromise()` | Value is a Promise | | ||
| `isRegExp()` | Value is a reguler expression | | ||
| `isSet()` | Value is a Set | | ||
| `isString()` | Value is a String | | ||
| `isUndefined()` | Value is `undefined` | |
const TypeInspect = require('./TypeInspect').TypeInspect | ||
const TypeDiff = require('./TypeDiff').TypeDiff | ||
const Matcher = require('./Matcher').Matcher | ||
@@ -7,2 +8,3 @@ module.exports = { | ||
TypeDiff: TypeDiff, | ||
Matcher: Matcher, | ||
inspect(val) { | ||
@@ -9,0 +11,0 @@ const typeInspect = new TypeInspect() |
@@ -30,4 +30,13 @@ 'use strict' | ||
diffItem.setKind(left.kind, right.kind) | ||
diffItem.setValues(left.value, right.value) | ||
if (typeof right.test === 'function') { | ||
if (right.test(left)) { | ||
diffItem.setValues(left.value, left.value) | ||
} else { | ||
diffItem.setValues(left.value, right.message) | ||
} | ||
} else { | ||
diffItem.setValues(left.value, right.value) | ||
} | ||
if (diffItem.isDifferent) { | ||
@@ -34,0 +43,0 @@ this.isDifferent = true |
const InspectItem = require('./InspectItem').InspectItem | ||
const Matcher = require('./Matcher').Matcher | ||
@@ -48,2 +49,6 @@ class TypeInspect { | ||
inspectAny (value, dept = this.dept) { | ||
if (value instanceof Matcher) { | ||
return value | ||
} | ||
const inspected = { | ||
@@ -50,0 +55,0 @@ type: typeof value, |
const inspect = require('inspect.js') | ||
const TypeDiff = require('../').TypeDiff | ||
const Matcher = require('../src/Matcher').Matcher | ||
const typeInspect = require('../').inspect | ||
@@ -452,2 +453,33 @@ | ||
}) | ||
it('set string matcher', () => { | ||
const left = typeInspect('Banana') | ||
const matcher = new Matcher() | ||
const right = typeInspect(matcher.isString()) | ||
const diff = typeDiff.diffItem(left, right) | ||
inspect(diff).isEql({ | ||
isDifferent: false, | ||
type: 'string', | ||
kind: 'string', | ||
value: 'Banana' | ||
}) | ||
}) | ||
it('set string matcher fail', () => { | ||
const left = typeInspect('Banana') | ||
const matcher = new Matcher() | ||
const right = typeInspect(matcher.isNumber()) | ||
const diff = typeDiff.diffItem(left, right) | ||
inspect(diff).isEql({ | ||
isDifferent: true, | ||
typeAdded: 'string', | ||
typeRemoved: 'number', | ||
kindAdded: 'string', | ||
kindRemoved: 'number', | ||
valueAdded: 'Banana', | ||
valueRemoved: 'Value is not of type Number' | ||
}) | ||
}) | ||
}) | ||
@@ -454,0 +486,0 @@ |
@@ -5,2 +5,3 @@ 'use strict' | ||
const TypeInspect = require('../').TypeInspect | ||
const Matcher = require('../').Matcher | ||
@@ -72,2 +73,48 @@ const is = require('is-supported') | ||
describe('inspectAny()', () => { | ||
it('inspects an object matcher', () => { | ||
const matcher = new Matcher() | ||
const obj = { | ||
name: matcher.isString() | ||
} | ||
const ts = new TypeInspect() | ||
const inspected = ts.inspectAny(obj) | ||
inspect(inspected).isObject() | ||
inspect(inspected).isEql({ | ||
type: 'object', | ||
kind: 'object', | ||
value: { | ||
name: { | ||
type: 'string', | ||
kind: 'string', | ||
test: inspect.match.func, | ||
message: 'Value is not of type String' | ||
} | ||
} | ||
}) | ||
}) | ||
it('inspects an array matcher', () => { | ||
const matcher = new Matcher() | ||
const arr = [ | ||
matcher.isString() | ||
] | ||
const ts = new TypeInspect() | ||
const inspected = ts.inspectAny(arr) | ||
inspect(inspected).isObject() | ||
inspect(inspected).isEql({ | ||
type: 'object', | ||
kind: 'array', | ||
value: [{ | ||
type: 'string', | ||
kind: 'string', | ||
test: inspect.match.func, | ||
message: 'Value is not of type String' | ||
}] | ||
}) | ||
}) | ||
}) | ||
describe('inspectObject()', () => { | ||
@@ -74,0 +121,0 @@ it('inspects an object', () => { |
74607
15.51%26
8.33%2290
14.44%201
29.68%