Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
},
"dependencies": {
"array-timsort": "^1.0.3",
"core-util-is": "^1.0.3",
"esprima": "^4.0.1"
}
}
3 changes: 1 addition & 2 deletions src/array.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const {isArray} = require('core-util-is')
const {sort} = require('array-timsort')

const {
Expand Down Expand Up @@ -217,7 +216,7 @@ class CommentArray extends Array {

items.forEach(item => {
const prev = length
length += isArray(item)
length += Array.isArray(item)
? item.length
: 1

Expand Down
23 changes: 12 additions & 11 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -47,6 +40,12 @@ const define = (target, key, value) => Object.defineProperty(target, key, {
configurable: true
})

/**
* @param {unknown} v
* @returns {v is NonNullable<object>}
*/
const is_object = v => typeof v === 'object' && v !== null

const copy_comments_by_kind = (
target, source, target_key, source_key, prefix, remove_source
) => {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions src/stringify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const {
isArray, isObject, isFunction, isNumber, isString
} = require('core-util-is')

const {
PREFIX_BEFORE_ALL,
PREFIX_BEFORE,
Expand All @@ -19,7 +15,9 @@ const {
COMMA,
EMPTY,

UNDEFINED
UNDEFINED,

is_object
} = require('./common')

// eslint-disable-next-line no-control-regex, no-misleading-character-class
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down
11 changes: 4 additions & 7 deletions test/array.test.js
Original file line number Diff line number Diff line change
@@ -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('..')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions test/stringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('..')

Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -111,7 +110,7 @@ OLD_CASES.forEach(name => {
3,
null
].forEach(space => {
const s = isString(space)
const s = typeof space === 'string'
? space.length
: space

Expand Down