Socket
Socket
Sign inDemoInstall

@ms-cloudpack/json-utilities

Package Overview
Dependencies
5
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.8 to 0.1.0

2

lib/index.d.ts

@@ -1,3 +0,3 @@

export { readJson, readJsonSync } from './readJson.js';
export { readJson, readJsonSync, type ReadJsonOptions } from './readJson.js';
export { writeJson, writeJsonSync } from './writeJson.js';
//# sourceMappingURL=index.d.ts.map
/**
* Options for `readJson` and `readJsonSync`.
*/
export interface ReadJsonOptions {
/**
* How to read the JSON file:
* - `strict`: Use `JSON.parse` (fastest, but does not give informative error messages)
* - `permissive`: Use `jju.parse` (slower, but allows comments and trailing commas, and gives
* informative error messages if `verbose` is true)
* @default 'strict'
*/
mode?: 'strict' | 'permissive';
/**
* If true, log a warning if the file can't be parsed (or exists but can't be read).
* This does NOT log a message if the file doesn't exist.
*/
verbose?: boolean;
}
/**
* Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.
*/
export declare function readJson<TData>(path: string): Promise<TData | undefined>;
export declare function readJson<TData>(path: string, options?: ReadJsonOptions): Promise<TData | undefined>;
/**

@@ -9,3 +27,3 @@ * Synchronously reads JSON from a path and returns the object, or undefined if it does not exist

*/
export declare function readJsonSync<TData>(path: string): TData | undefined;
export declare function readJsonSync<TData>(path: string, options?: ReadJsonOptions): TData | undefined;
//# sourceMappingURL=readJson.d.ts.map
import fs from 'fs';
// NOTE: Importing the whole module (not named exports) is required to make Jest mocks work elsewhere.
import fsPromises from 'fs/promises';
import jju from 'jju';
/**
* Reads JSON from a path and returns the object, or undefined if it does not exist or is unparsable.
*/
export async function readJson(path) {
let result = undefined;
export async function readJson(path, options = {}) {
if (!fs.existsSync(path)) {
return undefined;
}
try {
result = JSON.parse(await fsPromises.readFile(path, 'utf8'));
const contents = await fsPromises.readFile(path, 'utf8');
return parseJson({ contents, path, ...options });
}
catch {
/* no-op */
catch (err) {
if (options.verbose) {
console.warn(`Error reading ${path}: ${err.message || err}`);
}
}
return result;
return undefined;
}

@@ -21,12 +27,32 @@ /**

*/
export function readJsonSync(path) {
let result = undefined;
export function readJsonSync(path, options = {}) {
if (!fs.existsSync(path)) {
return undefined;
}
try {
result = JSON.parse(fs.readFileSync(path, 'utf8'));
const contents = fs.readFileSync(path, 'utf8');
return parseJson({ contents, path, ...options });
}
catch {
/* no-op */
catch (err) {
if (options.verbose) {
console.warn(`Error reading ${path}: ${err.message || err}`);
}
}
return result;
return undefined;
}
function parseJson(params) {
const { contents, path, mode, verbose } = params;
try {
if (mode === 'permissive') {
return jju.parse(contents);
}
return JSON.parse(contents);
}
catch (err) {
if (verbose) {
console.warn(`Error parsing JSON from ${path}: ${err.message || err}`);
}
}
return undefined;
}
//# sourceMappingURL=readJson.js.map

@@ -8,5 +8,5 @@ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.

"packageName": "@microsoft/api-extractor",
"packageVersion": "7.36.4"
"packageVersion": "7.37.1"
}
]
}
{
"name": "@ms-cloudpack/json-utilities",
"version": "0.0.8",
"version": "0.1.0",
"description": "Helpers for reading/writing json files.",

@@ -17,7 +17,9 @@ "license": "MIT",

"dependencies": {
"fs-extra": "^11.0.0"
"fs-extra": "^11.0.0",
"jju": "^1.4.0"
},
"devDependencies": {
"@ms-cloudpack/eslint-plugin-internal": "*",
"@ms-cloudpack/scripts": "*"
"@ms-cloudpack/scripts": "*",
"@types/jju": "^1.4.4"
},

@@ -29,3 +31,6 @@ "scripts": {

"lint:update": "cloudpack-scripts lint-update",
"lint": "cloudpack-scripts lint"
"lint": "cloudpack-scripts lint",
"test:update": "cloudpack-scripts test-update",
"test:watch": "cloudpack-scripts test-watch",
"test": "cloudpack-scripts test"
},

@@ -32,0 +37,0 @@ "files": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc