-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add readJsonFile/writeJsonFile/trimChars func
- Loading branch information
1 parent
48751bf
commit e698bb3
Showing
8 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import fs from 'node:fs' | ||
import { deepMerge } from '../object/deepMerge'; | ||
import { promises as fsPromises } from 'node:fs'; | ||
|
||
export function readJsonFile(jsonfile: string,defaultValue?:Record<string,any>) { | ||
let json = {} | ||
if(fs.existsSync(jsonfile)){ | ||
json = JSON.parse(fs.readFileSync(jsonfile,{encoding:'utf-8'}).toString()) | ||
} | ||
if(defaultValue){ | ||
json = deepMerge({},defaultValue,json) | ||
} | ||
return json | ||
} | ||
|
||
export async function readJsonFileAsync(jsonfile: string, defaultValue?: Record<string, any>) { | ||
let json = {}; | ||
if (await fsPromises.access(jsonfile).then(() => true).catch(() => false)) { | ||
json = JSON.parse((await fsPromises.readFile(jsonfile, { encoding: 'utf-8' })).toString()); | ||
} | ||
if (defaultValue) { | ||
json = deepMerge({}, defaultValue, json); | ||
} | ||
return json; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
import fs from 'node:fs'; | ||
import { deepMerge } from '../object/deepMerge'; | ||
import { promises as fsPromises } from 'node:fs'; | ||
|
||
export function writeJsonFile(jsonfile: string, data:Record<string,any>) { | ||
let json = {} | ||
if(fs.existsSync(jsonfile)){ | ||
json = JSON.parse(fs.readFileSync(jsonfile,{encoding:'utf-8'}).toString()) | ||
} | ||
json = deepMerge(json,data) | ||
fs.writeFileSync(jsonfile,JSON.stringify(json,null,4)) | ||
return json | ||
|
||
} | ||
|
||
export async function writeJsonFileAsync(jsonfile: string, data: Record<string, any>) { | ||
let json = {}; | ||
if (await fsPromises.stat(jsonfile).then(() => true).catch(() => false)) { | ||
const fileContent = await fsPromises.readFile(jsonfile, { encoding: 'utf-8' }); | ||
json = JSON.parse(fileContent.toString()); | ||
} | ||
json = deepMerge(json, data); | ||
await fsPromises.writeFile(jsonfile, JSON.stringify(json, null, 4)); | ||
return json; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
/** | ||
* 移除字符串前后的字符 | ||
* | ||
* trimChars(" hello world ") => "hello world" | ||
* trimChars(" \nhello world\n ") => "hello world" | ||
* | ||
* @param {*} str | ||
* @param {*} chars | ||
* @returns | ||
*/ | ||
export function trimChars(str:string,chars:string=`"'`){ | ||
let start = 0 | ||
let end = str.length | ||
const arrChars = chars.split('') | ||
while(start<end && arrChars.includes(str[start])){ | ||
start++ | ||
} | ||
while(end>start && arrChars.includes(str[end-1])){ | ||
end-- | ||
} | ||
return str.substring(start,end) | ||
} | ||
|
||
String.prototype.trimChars=function(this:string,chars:string){ | ||
return trimChars(this,chars) | ||
} | ||
|
||
declare global { | ||
interface String { | ||
trimChars(chars: string): string | ||
} | ||
} |