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
696 changes: 211 additions & 485 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions fixtures/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const jsoncst = require("jsoncst");
const { default: jsonmod, formatValue } = require("jsoncst");

console.log(jsoncst, jsoncst.replace);
console.log(jsonmod, formatValue);
4 changes: 2 additions & 2 deletions fixtures/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import jsoncst, { replace } from "jsoncst";
import jsonmod, { formatValue } from "jsoncst";

console.log(jsoncst, replace);
console.log(jsonmod, formatValue);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-codemod",
"version": "1.1.0",
"version": "2.0.0",
"private": false,
"description": "A utility to patch the JSON string and preserve the original formatting, including comments and whitespace.",
"sideEffects": false,
Expand Down
1 change: 0 additions & 1 deletion rslib.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class RspackDtsCopyPlugin {
};

copyDts(path.join(projectDir, "src"));
copyDts(path.join(projectDir, "src", "function"));
});
}
}
Expand Down
65 changes: 65 additions & 0 deletions src/JsonMod.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* JsonMod - A chainable API for modifying JSON strings while preserving formatting
*/
export declare class JsonMod {
/**
* Creates a new JsonMod instance
* @param sourceText - The JSON string to modify
*/
constructor(sourceText: string);

/**
* Replace a value at the specified path
* @param path - The JSON path or array of path segments
* @param value - The new value as a JSON string
* @returns Returns this for chaining
* @example
* jsonmod(source).replace("user.name", '"Bob"').apply()
* jsonmod(source).replace(["user", "name"], '"Bob"').apply()
*/
replace(path: string | string[], value: string): JsonMod;

/**
* Delete a property or array element at the specified path
* @param path - The JSON path or array of path segments
* @returns Returns this for chaining
* @example
* jsonmod(source).delete("user.age").apply()
* jsonmod(source).remove(["user", "age"]).apply()
*/
delete(path: string | string[]): JsonMod;

/**
* Alias for delete()
* @param path - The JSON path or array of path segments
* @returns Returns this for chaining
*/
remove(path: string | string[]): JsonMod;

/**
* Insert a new property into an object or element into an array
* @param path - The JSON path pointing to the object/array
* @param keyOrPosition - For objects: property name; For arrays: index
* @param value - The value to insert as a JSON string
* @returns Returns this for chaining
* @example
* jsonmod(source).insert("user", "email", '"test@example.com"').apply()
* jsonmod(source).insert("items", 0, '"newItem"').apply()
*/
insert(path: string | string[], keyOrPosition: string | number, value: string): JsonMod;

/**
* Apply all queued operations and return the modified JSON string
* @returns The modified JSON string
*/
apply(): string;
}

/**
* Factory function to create a new JsonMod instance
* @param sourceText - The JSON string to modify
* @returns A new JsonMod instance
* @example
* jsonmod(source).replace("a", "10").delete("b").apply()
*/
export declare function jsonmod(sourceText: string): JsonMod;
Loading