generated from cubetiq/ts-project
Task: Completed for cryptojs and tests functions and validate keys
This commit is contained in:
@@ -2,9 +2,13 @@ import * as fs from "fs"
|
||||
import * as path from "path"
|
||||
import { DEFAULT_ENCODING_TYPE } from "../config"
|
||||
|
||||
export const readFileStringDefaultEncoding = (
|
||||
export const readFileToStringDefaultEncoding = (
|
||||
relativeFilePath: string
|
||||
): string => {
|
||||
const absolutePath = path.resolve(relativeFilePath)
|
||||
return fs.readFileSync(absolutePath, DEFAULT_ENCODING_TYPE)
|
||||
}
|
||||
|
||||
export const readFileToJson = (relativeFilePath: string): any => {
|
||||
return JSON.parse(readFileToStringDefaultEncoding(relativeFilePath))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { DEFAULT_BUFFER_TYPE } from "../config"
|
||||
import { readFileStringDefaultEncoding } from "./file.util"
|
||||
import { DEFAULT_BUFFER_TYPE, DEFAULT_ENCODING_TYPE } from "../config"
|
||||
import { readFileToStringDefaultEncoding, readFileToJson } from "./file.util"
|
||||
import {
|
||||
assertNotNullOrUndefined,
|
||||
assertNotNullOrUndefinedOrEmpty,
|
||||
isNullOrUndefined,
|
||||
isNullOrUndefinedOrEmpty,
|
||||
} from "./test.util"
|
||||
|
||||
const stringToBuffer = (data: string | Buffer): Buffer => {
|
||||
if (data instanceof Buffer) {
|
||||
@@ -9,4 +15,21 @@ const stringToBuffer = (data: string | Buffer): Buffer => {
|
||||
}
|
||||
}
|
||||
|
||||
export { readFileStringDefaultEncoding as readFileString, stringToBuffer }
|
||||
const bufferToString = (data: string | Buffer): string => {
|
||||
if (data instanceof Buffer) {
|
||||
return data.toString(DEFAULT_ENCODING_TYPE)
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
readFileToStringDefaultEncoding as readFileToString,
|
||||
readFileToJson,
|
||||
stringToBuffer,
|
||||
bufferToString,
|
||||
assertNotNullOrUndefined,
|
||||
assertNotNullOrUndefinedOrEmpty,
|
||||
isNullOrUndefined,
|
||||
isNullOrUndefinedOrEmpty,
|
||||
}
|
||||
|
||||
22
src/util/test.util.ts
Normal file
22
src/util/test.util.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export const assertNotNullOrUndefined = (value: unknown, msg?: string) => {
|
||||
if (isNullOrUndefined(value)) {
|
||||
throw new Error(msg || "value is null or undefined")
|
||||
}
|
||||
}
|
||||
|
||||
export const assertNotNullOrUndefinedOrEmpty = (
|
||||
value: unknown,
|
||||
msg?: string
|
||||
) => {
|
||||
if (isNullOrUndefinedOrEmpty(value)) {
|
||||
throw new Error(msg || "value is null or undefined or empty")
|
||||
}
|
||||
}
|
||||
|
||||
export const isNullOrUndefined = (value: unknown): boolean => {
|
||||
return value === null || value === undefined
|
||||
}
|
||||
|
||||
export const isNullOrUndefinedOrEmpty = (value: unknown): boolean => {
|
||||
return isNullOrUndefined(value) || value === ""
|
||||
}
|
||||
Reference in New Issue
Block a user