comment-json
Advanced tools
Comparing version 4.0.6 to 4.1.0
{ | ||
"name": "comment-json", | ||
"version": "4.0.6", | ||
"version": "4.1.0", | ||
"description": "Parse and stringify JSON with comments. It will retain comments even after saved!", | ||
@@ -53,11 +53,12 @@ "main": "src/index.js", | ||
"@ostai/eslint-config": "^3.5.0", | ||
"ava": "^3.12.1", | ||
"ava": "^3.13.0", | ||
"codecov": "^3.7.2", | ||
"eslint": "^7.8.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint": "^7.10.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"nyc": "^15.1.0", | ||
"test-fixture": "^2.4.1", | ||
"typescript": "^4.0.2" | ||
"typescript": "^4.0.3" | ||
}, | ||
"dependencies": { | ||
"array-timsort": "^1.0.3", | ||
"core-util-is": "^1.0.2", | ||
@@ -64,0 +65,0 @@ "esprima": "^4.0.1", |
108
src/array.js
@@ -1,3 +0,3 @@ | ||
const hasOwnProperty = require('has-own-prop') | ||
const {isArray} = require('core-util-is') | ||
const {sort} = require('array-timsort') | ||
@@ -10,25 +10,7 @@ const { | ||
symbol, | ||
define, | ||
copy_comments | ||
copy_comments, | ||
swap_comments | ||
} = require('./common') | ||
const swap_comments = (array, from, to) => { | ||
if (from === to) { | ||
return | ||
} | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
const target_prop = symbol(prefix, to) | ||
if (!hasOwnProperty(array, target_prop)) { | ||
copy_comments(array, array, to, from, prefix) | ||
return | ||
} | ||
const comments = array[target_prop] | ||
copy_comments(array, array, to, from, prefix) | ||
define(array, symbol(prefix, from), comments) | ||
}) | ||
} | ||
const reverse_comments = array => { | ||
@@ -45,5 +27,3 @@ const {length} = array | ||
const move_comment = (target, source, i, offset, remove) => { | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
copy_comments(target, source, i + offset, i, prefix, remove) | ||
}) | ||
copy_comments(target, source, i + offset, i, remove) | ||
} | ||
@@ -75,3 +55,3 @@ | ||
while (i -- > 0) { | ||
move_comment(target, source, start + i, offset, remove && i < offset) | ||
move_comment(target, source, start + i, offset, remove) | ||
} | ||
@@ -82,3 +62,2 @@ return | ||
let i = 0 | ||
const min_remove = count + offset | ||
// | remove | count | | ||
@@ -92,6 +71,24 @@ // ------------- | ||
const ii = i ++ | ||
move_comment(target, source, start + ii, offset, remove && i >= min_remove) | ||
move_comment(target, source, start + ii, offset, remove) | ||
} | ||
} | ||
const remove_comments = (array, key) => { | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
const prop = symbol(prefix, key) | ||
delete array[prop] | ||
}) | ||
} | ||
const get_mapped = (map, key) => { | ||
let mapped = key | ||
while (mapped in map) { | ||
mapped = map[mapped] | ||
} | ||
return mapped | ||
} | ||
class CommentArray extends Array { | ||
@@ -195,2 +192,3 @@ // - deleteCount + items.length | ||
remove_comments(this, 0) | ||
move_comments(this, this, 1, length, - 1, true) | ||
@@ -213,7 +211,3 @@ | ||
// Removes comments | ||
const {length} = this | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
const prop = symbol(prefix, length) | ||
delete this[prop] | ||
}) | ||
remove_comments(this, this.length) | ||
@@ -246,6 +240,56 @@ return ret | ||
} | ||
sort (...args) { | ||
const result = sort( | ||
this, | ||
// Make sure there is no more than one argument | ||
...args.slice(0, 1) | ||
) | ||
// For example, | ||
// if we sort ['b', 'd', 'c', 'a'], | ||
// then `result` will be [3, 0, 2, 1], and the array is ['a', 'b', 'c', 'd'] | ||
// First, we swap index 0 (b) and index 3 (a), then the array comments are | ||
// ['a.comments', 'd.comments', 'c.comments', 'b.comments'] | ||
// index 0 is finalized | ||
// index 3 is actually mapped to original index 0, we present as 0 -> 3 | ||
// Then swap index 1 (d) and index 0 (-> 3, b) | ||
// 1 (index) -> 0 (new index) -> 3 (real_index) | ||
// ['d.comments', 'b.comments', 'c.comments', 'd.comments'] | ||
// index 1 is finalized | ||
// index 3 is contains the item of original index 1 | ||
// - we present as 1 -> 3 | ||
// - it is ok that we don't remove mapping 0 -> 3 | ||
// Then index 2 should be skipped | ||
// Then swap index 3 (d) and index 1 (-> 3, b), skipped | ||
const map = Object.create(null) | ||
result.forEach((source_index, index) => { | ||
if (source_index === index) { | ||
return | ||
} | ||
const real_source_index = get_mapped(map, source_index) | ||
if (real_source_index === index) { | ||
return | ||
} | ||
// The item of index `index` gets the final value | ||
// delete map[index] | ||
map[index] = real_source_index | ||
swap_comments(this, index, real_source_index) | ||
}) | ||
} | ||
} | ||
module.exports = { | ||
CommentArray | ||
} |
@@ -48,3 +48,3 @@ const hasOwnProperty = require('has-own-prop') | ||
const copy_comments = ( | ||
const copy_comments_by_kind = ( | ||
target, source, target_key, source_key, prefix, remove_source | ||
@@ -68,2 +68,32 @@ ) => { | ||
const copy_comments = ( | ||
target, source, target_key, source_key, remove_source | ||
) => { | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
copy_comments_by_kind( | ||
target, source, target_key, source_key, prefix, remove_source | ||
) | ||
}) | ||
} | ||
const swap_comments = (array, from, to) => { | ||
if (from === to) { | ||
return | ||
} | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
const target_prop = symbol(prefix, to) | ||
if (!hasOwnProperty(array, target_prop)) { | ||
copy_comments_by_kind(array, array, to, from, prefix, true) | ||
return | ||
} | ||
const comments = array[target_prop] | ||
delete array[target_prop] | ||
copy_comments_by_kind(array, array, to, from, prefix, true) | ||
define(array, symbol(prefix, from), comments) | ||
}) | ||
} | ||
const assign_non_prop_comments = (target, source) => { | ||
@@ -87,5 +117,3 @@ NON_PROP_SYMBOL_KEYS.forEach(key => { | ||
target[key] = source[key] | ||
SYMBOL_PREFIXES.forEach(prefix => { | ||
copy_comments(target, source, key, key, prefix) | ||
}) | ||
copy_comments(target, source, key, key) | ||
}) | ||
@@ -124,2 +152,3 @@ | ||
copy_comments, | ||
swap_comments, | ||
assign_non_prop_comments, | ||
@@ -126,0 +155,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45271
1078
5
+ Addedarray-timsort@^1.0.3
+ Addedarray-timsort@1.0.3(transitive)