Skip to content
Merged
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 @@ -248,7 +247,7 @@ class CommentArray extends Array {

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

Expand Down
24 changes: 12 additions & 12 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
const {
isObject,
isArray,
isString,
isNumber,
isFunction
} = 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,12 +39,12 @@
const LINE_BREAKS_AFTER = new WeakMap()
const RAW_STRING_LITERALS = new WeakMap()

const normalize_key = key => isString(key) || isNumber(key)

Check failure on line 42 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isNumber' is not defined

Check failure on line 42 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isString' is not defined

Check failure on line 42 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isNumber' is not defined

Check failure on line 42 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isString' is not defined
? String(key)
: null

const set_raw_string_literal = (host, key, raw) => {
if (!isObject(host) || !isString(raw)) {

Check failure on line 47 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isString' is not defined

Check failure on line 47 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isObject' is not defined

Check failure on line 47 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isString' is not defined

Check failure on line 47 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isObject' is not defined
return
}

Expand All @@ -71,7 +63,7 @@
}

const get_raw_string_literal = (host, key) => {
if (!isObject(host)) {

Check failure on line 66 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isObject' is not defined

Check failure on line 66 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isObject' is not defined
return
}

Expand Down Expand Up @@ -111,6 +103,12 @@
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 @@ -173,7 +171,7 @@
// 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 All @@ -188,7 +186,7 @@
return target
}

const is_raw_json = isFunction(JSON.isRawJSON)

Check failure on line 189 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isFunction' is not defined

Check failure on line 189 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isFunction' is not defined
// For backward compatibility,
// since JSON.isRawJSON is not supported in node < 21
? JSON.isRawJSON
Expand All @@ -196,11 +194,11 @@
: () => false

const set_comment_line_breaks = (comment, before, after) => {
if (isNumber(before) && before >= 0) {

Check failure on line 197 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isNumber' is not defined

Check failure on line 197 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isNumber' is not defined
LINE_BREAKS_BEFORE.set(comment, before)
}

if (isNumber(after) && after >= 0) {

Check failure on line 201 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isNumber' is not defined

Check failure on line 201 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isNumber' is not defined
LINE_BREAKS_AFTER.set(comment, after)
}
}
Expand Down Expand Up @@ -238,6 +236,8 @@
swap_comments,
assign_non_prop_comments,

is_object,

is_raw_json,
set_raw_string_literal,
get_raw_string_literal,
Expand Down Expand Up @@ -274,11 +274,11 @@
* assign(target, source, [])
*/
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 @@ -291,7 +291,7 @@
// 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) {
// Copy all non-property comments from source to target
Expand Down Expand Up @@ -349,7 +349,7 @@
where: to_where,
key: to_key
}, override = false) {
if (!isObject(source)) {

Check failure on line 352 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isObject' is not defined

Check failure on line 352 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isObject' is not defined
throw new TypeError('source must be an object')
}

Expand All @@ -357,7 +357,7 @@
target = source
}

if (!isObject(target)) {

Check failure on line 360 in src/common.js

View workflow job for this annotation

GitHub Actions / build (24.x)

'isObject' is not defined

Check failure on line 360 in src/common.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'isObject' is not defined
// No target to move to
return
}
Expand Down
23 changes: 11 additions & 12 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 @@ -20,6 +16,9 @@ const {
EMPTY,

UNDEFINED,

is_object,

get_raw_string_literal,
get_comment_line_breaks_before,
get_comment_line_breaks_after,
Expand Down Expand Up @@ -267,7 +266,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 @@ -332,13 +331,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 @@ -365,7 +364,7 @@ function stringify (key, holder, gap) {
return value.rawJSON
}

return isArray(value)
return Array.isArray(value)
? array_stringify(value, gap)
: object_stringify(value, gap)

Expand All @@ -376,10 +375,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 @@ -442,7 +441,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 @@ -455,7 +454,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('..')
const {
Expand Down Expand Up @@ -76,7 +75,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, replacer)
)
Expand Down Expand Up @@ -141,7 +140,7 @@ OLD_CASES.forEach(name => {
3,
null
].forEach(space => {
const s = isString(space)
const s = typeof space === 'string'
? space.length
: space

Expand Down
Loading