diff --git a/package.json b/package.json index d086875..c4766fc 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ }, "dependencies": { "array-timsort": "^1.0.3", - "core-util-is": "^1.0.3", "esprima": "^4.0.1" } } diff --git a/src/array.js b/src/array.js index 17ee0d4..b6403e3 100644 --- a/src/array.js +++ b/src/array.js @@ -1,4 +1,3 @@ -const {isArray} = require('core-util-is') const {sort} = require('array-timsort') const { @@ -217,7 +216,7 @@ class CommentArray extends Array { items.forEach(item => { const prev = length - length += isArray(item) + length += Array.isArray(item) ? item.length : 1 diff --git a/src/common.js b/src/common.js index 8f886e2..354724a 100644 --- a/src/common.js +++ b/src/common.js @@ -1,10 +1,3 @@ -const { - isObject, - isArray, - isString, - isNumber -} = require('core-util-is') - const PREFIX_BEFORE = 'before' const PREFIX_AFTER_PROP = 'after-prop' const PREFIX_AFTER_COLON = 'after-colon' @@ -47,6 +40,12 @@ const define = (target, key, value) => Object.defineProperty(target, key, { configurable: true }) +/** + * @param {unknown} v + * @returns {v is NonNullable} + */ +const is_object = v => typeof v === 'object' && v !== null + const copy_comments_by_kind = ( target, source, target_key, source_key, prefix, remove_source ) => { @@ -109,7 +108,7 @@ const assign_non_prop_comments = (target, source) => { // Assign keys and comments const assign = (target, source, keys) => { keys.forEach(key => { - if (!isString(key) && !isNumber(key)) { + if (typeof key !== 'string' && typeof key !== 'number') { return } @@ -154,12 +153,14 @@ module.exports = { swap_comments, assign_non_prop_comments, + is_object, + assign (target, source, keys) { - if (!isObject(target)) { + if (!is_object(target)) { throw new TypeError('Cannot convert undefined or null to object') } - if (!isObject(source)) { + if (!is_object(source)) { return target } @@ -168,7 +169,7 @@ module.exports = { // We assign non-property comments // if argument `keys` is not specified assign_non_prop_comments(target, source) - } else if (!isArray(keys)) { + } else if (!Array.isArray(keys)) { throw new TypeError('keys must be array or undefined') } else if (keys.length === 0) { // Or argument `keys` is an empty array diff --git a/src/stringify.js b/src/stringify.js index 80a35d7..3939f6a 100644 --- a/src/stringify.js +++ b/src/stringify.js @@ -1,7 +1,3 @@ -const { - isArray, isObject, isFunction, isNumber, isString -} = require('core-util-is') - const { PREFIX_BEFORE_ALL, PREFIX_BEFORE, @@ -19,7 +15,9 @@ const { COMMA, EMPTY, - UNDEFINED + UNDEFINED, + + is_object } = require('./common') // eslint-disable-next-line no-control-regex, no-misleading-character-class @@ -198,7 +196,7 @@ const object_stringify = (value, gap) => { let after_comma = EMPTY let first = true - const keys = isArray(replacer) + const keys = Array.isArray(replacer) ? replacer : Object.keys(value) @@ -263,13 +261,13 @@ function stringify (key, holder, gap) { let value = holder[key] // If the value has a toJSON method, call it to obtain a replacement value. - if (isObject(value) && isFunction(value.toJSON)) { + if (is_object(value) && typeof value.toJSON === 'function') { value = value.toJSON(key) } // If we were called with a replacer function, then call the replacer to // obtain a replacement value. - if (isFunction(replacer)) { + if (typeof replacer === 'function') { value = replacer.call(holder, key, value) } @@ -292,7 +290,7 @@ function stringify (key, holder, gap) { // If the type is 'object', we might be dealing with an object or an array or // null. case 'object': - return isArray(value) + return Array.isArray(value) ? array_stringify(value, gap) : object_stringify(value, gap) @@ -303,10 +301,10 @@ function stringify (key, holder, gap) { } } -const get_indent = space => isString(space) +const get_indent = space => typeof space === 'string' // If the space parameter is a string, it will be used as the indent string. ? space - : isNumber(space) + : typeof space === 'number' ? SPACE.repeat(space) : EMPTY @@ -344,7 +342,7 @@ module.exports = (value, replacer_, space) => { } // vanilla `JSON.parse` allow invalid replacer - if (!isFunction(replacer_) && !isArray(replacer_)) { + if (typeof replacer_ !== 'function' && !Array.isArray(replacer_)) { replacer_ = null } @@ -357,7 +355,7 @@ module.exports = (value, replacer_, space) => { clean() - return isObject(value) + return is_object(value) ? process_comments(value, PREFIX_BEFORE_ALL, EMPTY, true).trimLeft() + str + process_comments(value, PREFIX_AFTER_ALL, EMPTY).trimRight() diff --git a/test/array.test.js b/test/array.test.js index 4754122..36b80a6 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -1,8 +1,5 @@ // eslint-disable-next-line import/no-unresolved const test = require('ava') -const { - isFunction, isObject, isString, isArray -} = require('core-util-is') const { parse, stringify, assign, CommentArray } = require('..') @@ -41,13 +38,13 @@ const texpect = ( // real return value rr ) => { - if (isObject(ret)) { + if (typeof ret === 'object' && ret !== null) { t.deepEqual(r, ret) } else { t.is(r, ret) } - if (isString(rr)) { + if (typeof rr === 'string') { t.is(rr, str) } else { t.is(st(rr), str) @@ -305,7 +302,7 @@ CASES.forEach(([d, a, run, e, s]) => { const parsed = parse(a) const ret = run(parsed) - const expect = isFunction(e) + const expect = typeof e === 'function' ? e : (tt, r, str) => { tt.deepEqual(r, e) @@ -315,7 +312,7 @@ CASES.forEach(([d, a, run, e, s]) => { expect( t, // Cleaned return value - isArray(ret) + Array.isArray(ret) // clean ret ? [...ret] : ret, diff --git a/test/stringify.test.js b/test/stringify.test.js index c7b0016..39b1673 100644 --- a/test/stringify.test.js +++ b/test/stringify.test.js @@ -2,7 +2,6 @@ const test = require('ava') const {resolve} = require('test-fixture')() const fs = require('fs') -const {isFunction, isString} = require('core-util-is') const {parse, stringify} = require('..') @@ -69,7 +68,7 @@ const each = (subjects, replacers, spaces, iterator) => { spaces.forEach((space, iii) => { const desc = [subject, replacer, space] .map(s => - isFunction(s) + typeof s === 'function' ? 'replacer' : JSON.stringify(s) ) @@ -111,7 +110,7 @@ OLD_CASES.forEach(name => { 3, null ].forEach(space => { - const s = isString(space) + const s = typeof space === 'string' ? space.length : space