fs-extender / Json
▸ ensureJsonFile(path
, obj
, options
, callback
): void
Converts a JavaScript value to a JavaScript Object Notation (JSON) string and write it to a file making sure the file is created even if the path doesn't exist. This works as a mix between writeJsonFile and ensureFile
import * as fs from "fs-extender";
fs.ensureJsonFile(path, { name: "Jonh Smith", age: 1001 }, (err) => {
if (!err) {
console.log(`File writed with success`);
}
});
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
unknown |
object to write to file as json string |
options |
WriteJsonOptions |
WriteJsonOptions |
callback |
(err : null | ErrnoException , path : PathLike ) => void |
(err: NodeJs.ErroNoException | null, path: fs.PathLike) |
void
▸ ensureJsonFile(path
, obj
, callback
): void
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
unknown |
object to write to file as json string |
callback |
(err : null | ErrnoException , path : PathLike ) => void |
(err: NodeJs.ErroNoException | null, path: fs.PathLike) |
void
▸ ensureJsonFileSync(path
, obj
, options?
): PathLike
Converts a JavaScript value to a JavaScript Object Notation (JSON) string and write it to a file making sure the file is created even if the path doesn't exist This works as a mix between writeJsonFileSync and ensureFileSync
import * as fs from "fs-extender";
fs.ensureJsonFileSync(path, { name: "Jonh Smith", age: 1001 });
console.log(`File writed with success`);
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
unknown |
object to write to file as json string |
options? |
WriteJsonOptions |
WriteJsonOptions |
fs.PathLike
▸ readJsonFile<T
>(path
, options
, callback
): void
Read json file and transform's it into an object
import * as fs from "fs-extender";
fs.readJsonFile(path, (err, obj) => {
if (!err) {
console.log(`File read with success. Object: ${obj}`);
}
});
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
options |
ReadJsonOptions |
ReadJsonOptions |
callback |
(err : null | ErrnoException , res : T ) => void |
(err: NodeJs.ErrNoException | null, res: any) |
void
▸ readJsonFile<T
>(path
, callback
): void
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
callback |
(err : null | ErrnoException , res : T ) => void |
(err: NodeJs.ErrNoException | null, res: any) |
void
▸ readJsonFileSync<T
>(path
, options?
): T
Read json file and transform's it into an object
import * as fs from "fs-extender";
const obj = fs.readJsonFileSync(path);
console.log(`File read with success. Object: ${obj}`);
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
options? |
ReadJsonOptions |
ReadJsonOptions |
T
any
▸ readJsonLines(path
, options
, fn
, callback
): void
Read json file and transform's it into an object
import * as fs from "fs-extender";
const lines = [];
fs.readJsonLines(
path,
(obj) => {
lines.push(obj);
return true;
},
(err) => {
if (!err) {
console.log(`File read with success. Lines: ${lines.length}`);
}
}
);
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
options |
ReadJsonLineOptions |
ReadJsonLineOptions |
fn |
ReadJsonLinesFunction |
function executed for each line readed: (obj: any) => boolean | Promise<boolean> if the function return false the execution will be stopped. |
callback |
(err : null | ErrnoException ) => void |
(err: NodeJs.ErrNoException | null, res: any) |
void
▸ readJsonLines(path
, fn
, callback
): void
| Name | Type | Description |
| :-- | :-- | :-- | --- |
| path
| PathLike
| | path to file |
| fn
| ReadJsonLinesFunction
| function executed for each line readed: (obj: any) => boolean \| Promise<boolean>
if the function return false the execution will be stopped. |
| callback
| (err
: null
| ErrnoException
) => void
| (err: NodeJs.ErrNoException \| null, res: any)
|
void
▸ writeJsonFile<T
>(path
, obj
, options
, callback
): void
Converts a JavaScript value to a JavaScript Object Notation (JSON) string and write it to a file
import * as fs from "fs-extender";
fs.writeJsonFile(path, { name: "Jonh Smith", age: 1001 }, (err) => {
if (!err) {
console.log(`File writed with success`);
}
});
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
T |
object to write to file as json string |
options |
WriteJsonOptions |
WriteJsonOptions |
callback |
(err : null | ErrnoException , path : PathLike ) => void |
(err: NodeJs.ErroNoException | null, path: fs.PathLike) |
void
▸ writeJsonFile<T
>(path
, obj
, callback
): void
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
T |
object to write to file as json string |
callback |
(err : null | ErrnoException , path : PathLike ) => void |
(err: NodeJs.ErroNoException | null, path: fs.PathLike) |
void
▸ writeJsonFileSync(path
, obj
, options?
): PathLike
Converts a JavaScript value to a JavaScript Object Notation (JSON) string and write it to a file
import * as fs from "fs-extender";
fs.writeJsonFileSync(path, { name: "Jonh Smith", age: 1001 });
console.log(`File writed with success`);
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
unknown |
object to write to file as json string |
options? |
WriteJsonOptions |
WriteJsonOptions |
▸ fsPromises.ensureJsonFile(path
, obj
, options
): void
Converts a JavaScript value to a JavaScript Object Notation (JSON) string and write it to a file making sure the file is created even if the path doesn't exist This works as a mix between fsPromises.writeJsonFile and fsPromises.ensureFile
import * as fs from "fs-extender";
await fs.promises.ensureJsonFile(path, { name: "Jonh Smith", age: 1001 });
console.log(`File writed with success`);
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
unknown |
object to write to file as json string |
options? |
WriteJsonOptions |
WriteJsonOptions |
void
▸ fsPromises.readJsonFile<T
>(path
, options
): void
Read json file and transform's it into an object
import * as fs from "fs-extender";
await fs.promises.readJsonFile(path);
console.log(`File read with success. Object: ${obj}`);
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
options |
ReadJsonOptions |
ReadJsonOptions |
void
▸ fsPromises.readJsonLines(path
, options
, fn
): void
Read json file and transform's it into an object
import * as fs from "fs-extender";
const lines = [];
await fs.promises.readJsonLines(path, (obj) => {
lines.push(obj);
return true;
});
console.log(`File read with success. Lines: ${lines.length}`);
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
options |
ReadJsonLineOptions |
ReadJsonLineOptions |
fn |
ReadJsonLinesFunction |
function executed for each line readed: (obj: any) => boolean | Promise<boolean> if the function return false the execution will be stopped. |
void
▸ fsPromises.readJsonLines(path
, fn
): void
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
fn |
ReadJsonLinesFunction |
function executed for each line readed: (obj: any) => boolean | Promise<boolean> if the function return false the execution will be stopped. |
void
▸ fsPromises.writeJsonFile<T
>(path
, obj
, options
): void
Converts a JavaScript value to a JavaScript Object Notation (JSON) string and write it to a file
import * as fs from "fs-extender";
await fs.promises.writeJsonFile(path, { name: "Jonh Smith", age: 1001 });
console.log(`File writed with success`);
Name |
---|
T |
Name | Type | Description |
---|---|---|
path |
PathLike |
path to file |
obj |
T |
object to write to file as json string |
options? |
WriteJsonOptions |
WriteJsonOptions |
void
Ƭ ReplacerType: (key
: string
, value
: any
) => any
| (string
| number
)[] | null
Ƭ ReadJsonOptions: Object
Name | Type | Description |
---|---|---|
encoding? |
BufferEncoding |
BufferEncoding used to read the file, default: utf8 |
reviver? |
ReviverType |
Reviver function used to parse the json string, default: undefined |
throws? |
boolean |
should throw if an error occur, default: true |
flag? |
string |
flag used to open the file, default: r |
Ƭ ReadJsonLineOptions: Object
Name | Type | Description |
---|---|---|
encoding? |
BufferEncoding |
BufferEncoding used to read the file, default: utf8 |
reviver? |
ReviverType |
Reviver function used to parse the json string, default: undefined |
throws? |
boolean |
should throw if an error occur, default: true |
Ƭ ReadJsonLinesFunction: <T>(obj?
: T
) => boolean
| Promise
<boolean
>
▸ <T
>(obj?
): boolean
| Promise
<boolean
>
return boolean
, if false will stop execution
Name |
---|
T |
Name | Type |
---|---|
obj? |
T |
boolean
| Promise
<boolean
>
Ƭ ReviverType: (key
: string
, value
: any
) => any
▸ (key
, value
): any
Name | Type |
---|---|
key |
string |
value |
any |
any
Ƭ WriteJsonOptions: Object
Name | Type | Description |
---|---|---|
EOL? |
string |
End Of Line character default: \n |
encoding? |
BufferEncoding | null |
The encoding used to write the file, default: utf8 |
finalEOL? |
boolean |
Use EOL character at the end of the file, default: true |
flag? |
string |
Flag to be used when writing the file, default: wx , Open file for writing but fails if path exists |
mode? |
number | string |
The mode used to for the file, default: 0o666 |
replacer? |
ReplacerType |
A function that transforms the results or an array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified. |
spaces? |
number | string | null |
Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read. |