cometa/util.ts

87 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-01-19 18:52:30 +01:00
import * as fs from 'fs'
import * as crypto from 'crypto'
2018-02-06 20:00:09 +01:00
declare global {
interface String {
log: () => void
}
}
String.prototype.log = function (): void {
console.log(this)
}
2018-01-19 18:52:30 +01:00
export function readFile(url: string): Promise<string> {
return new Promise(res => {
fs.readFile(url, (err, data) => {
if (err)
throw new Error(`No such file: ${url}`)
else
res(data.toString())
})
})
}
2018-02-06 20:00:09 +01:00
export function readFileSync(url: string): string {
return fs.readFileSync(url).toString()
}
export function writeFile(url: string, data: any): Promise<boolean> {
return new Promise(res => {
fs.writeFile(url, data, err => {
if (err)
res(false)
res(true)
})
})
}
export function writeFileSync(url: string, data: any): void {
fs.writeFileSync(url, data)
}
export function fileExists(url: string): Promise<boolean> {
2018-01-19 18:52:30 +01:00
return new Promise(res => {
fs.exists(url, _ => {
res(_)
})
})
}
export function checksum(url: string, plain = false, alg = 'sha1'): Promise<string> {
return new Promise(res => {
const hash = crypto.createHash(alg)
if (plain) {
res(hash.update(url).digest('hex'))
}
else {
const stream = fs.createReadStream(url)
stream.on('data', data => hash.update(data, 'utf8'))
stream.on('end', _ => { res(hash.digest('hex')) })
}
})
2018-02-06 20:00:09 +01:00
}
export function replaceBetween(start: number, end: number, str: string, replace: string): string {
return str.substring(0, start) + replace + str.substring(end)
}
export function getFromObject(data: any, name: string): any {
name = name.trim()
// If not matches the valid pattern of a getter, return empty string
const valid: boolean = /^[A-z]\w*(\.[A-z]\w*|\[\d+\]|\[('|")\w+\2\]|\[[A-z]\w*\])*$/.test(name)
if (!valid)
return ''
name = name.replace(/('|")/g, '')
name = name.replace(/\[(\w+)\]/g, '.$1')
for (const i of name.split('.'))
data = data[i]
return data
2018-01-19 18:52:30 +01:00
}