Skip to content

Commit

Permalink
refactor(package): replace lodash.hasin
Browse files Browse the repository at this point in the history
Replace module lodash.hasin (4 KB) with private
function objectHasPropertyName (500 B)

#97
  • Loading branch information
gregswindle committed Sep 7, 2019
1 parent ea5711f commit 035db0f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/type-inspector/helpers/is-type-of.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const hasIn = require('lodash.hasin')
const objectHasPropertyName = require('./object-has-property-name')
const typeMap = require('./type-map')

const isTypeOf = (value, type) => {
if (hasIn(value, 'type')) {
if (objectHasPropertyName(value)) {
return value.type === type
}
return typeMap.get(type)(value)
Expand Down
6 changes: 3 additions & 3 deletions lib/type-inspector/helpers/is-type.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const get = require('lodash.get')
const hasIn = require('lodash.hasin')
const objectHasPropertyName = require('./object-has-property-name')
const types = require('./types.json')

const isType = (object, type) =>
hasIn(object, 'type') && object.type === get(types, type)
const isType = (instance, type) =>
objectHasPropertyName(instance) && instance.type === get(types, type)

module.exports = isType
10 changes: 5 additions & 5 deletions lib/type-inspector/helpers/is-value-type-of.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const hasIn = require('lodash.hasin')
const objectHasPropertyName = require('./object-has-property-name')
const typeMap = require('./type-map')

const isValueTypeOf = (value, type) => {
if (hasIn(value, 'value')) {
return typeMap.get(type)(value.value)
const isValueTypeOf = (instance, type) => {
if (objectHasPropertyName(instance, 'value')) {
return typeMap.get(type)(instance.value)
}
return typeMap.get(type)(value)
return typeMap.get(type)(instance)
}

module.exports = isValueTypeOf
21 changes: 21 additions & 0 deletions lib/type-inspector/helpers/object-has-property-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const {
isValid,
object
} = require('ow')

/**
* Determine whether a value is an object with a given property.
*
* @private
* @param {*} instance - An ECMAScript variable.
* @param {string} [propertyName='type'] - The name of the property
* being searched for.
* @returns {boolean} - `true` if the property exists; otherwise, `false`.
*/

const objectHasPropertyName = (instance, propertyName = 'type') => {
return isValid(instance, object) &&
Object.getOwnPropertyNames(instance).includes(propertyName)
}

module.exports = objectHasPropertyName
4 changes: 2 additions & 2 deletions lib/type-inspector/helpers/type-of.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const hasIn = require('lodash.hasin')
const is = require('@sindresorhus/is')
const objectHasPropertyName = require('./object-has-property-name')

const typeOf = (value) => {
if (hasIn(value, 'type')) {
if (objectHasPropertyName(value)) {
return value.type
}
return is(value)
Expand Down
10 changes: 5 additions & 5 deletions lib/type-inspector/helpers/value-type-of.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const hasIn = require('lodash.hasin')
const is = require('@sindresorhus/is')
const objectHasPropertyName = require('./object-has-property-name')

const valueTypeOf = (value) => {
if (hasIn(value, 'value')) {
return is(value.value)
const valueTypeOf = (instance) => {
if (objectHasPropertyName(instance, 'value')) {
return is(instance.value)
}
return is(value)
return is(instance)
}

module.exports = valueTypeOf

0 comments on commit 035db0f

Please sign in to comment.