Options
All
  • Public
  • Public/Protected
  • All
Menu

Module panacea-node

Index

Functions Crypto

Functions FS

Functions Module

Functions Process

Functions Random

Functions String

Functions Type

Functions fs

Crypto Functions

  • hashNode(value: string): Promise<unknown>
  • Creates a hash for a value using the SHA-256 algorithm. Returns a promise.

    example
    hashNode(JSON.stringify({ a: 'a', b: [1, 2, 3, 4], foo: { c: 'bar' } })).then(
    console.log
    );
    // '04aa106279f5977f59f9067fa9712afc4aedc6f5862a8defc34552d8c7206393'
    since

    0.1.0

    Parameters

    • value: string

      The value to hash.

    Returns Promise<unknown>

    A promise that resolves to the hash.

FS Functions

  • JSONToFile(obj: Record<string, any>, filename: string): void
  • Writes a JSON object to a file.

    example
    JSONToFile({ test: 'is passed' }, 'testJsonFile');
    
    since

    0.1.0

    Parameters

    • obj: Record<string, any>

      The object to write.

    • filename: string

      The file to write to.

    Returns void

  • createDirIfNotExists(dir: PathLike): void
  • Creates a directory if it does not exist.

    example
    createDirIfNotExists('test');
    // creates the directory 'test', if it doesn't exist
    since

    0.1.0

    Parameters

    • dir: PathLike

      The directory to create.

    Returns void

Module Functions

  • requireUncached(module: string): any
  • Loads a module after removing it from the cache (if exists).

    throws

    An error if the module is not valid or cannot be loaded.

    example
    const fs = requireUncached('fs'); // 'fs' will be loaded fresh every time
    
    since

    0.1.0

    Parameters

    • module: string

      The module to load.

    Returns any

    The loaded module.

Process Functions

  • getCmdArgs(): string[]
  • Gets the command-line arguments passed to a Node.js script.

    example
    // node my-script.js --name=John --age=30
    getCmdArgs(); // ['--name=John', '--age=30']
    since

    0.1.2

    Returns string[]

    a string array containing the command-line arguments

  • hasFlags(...flags: string[]): boolean
  • Checks if the current process's arguments contain the specified flags.

    example
    // node myScript.js -s --test --cool=true
    hasFlags('-s'); // true
    hasFlags('--test', 'cool=true', '-s'); // true
    hasFlags('special'); // false
    since

    0.1.0

    Parameters

    • Rest ...flags: string[]

      The flags to check for.

    Returns boolean

    true if the flag is contained in the arguments, false otherwise.

Random Functions

  • UUIDGeneratorNode(): string

String Functions

  • atob(string: string): string
  • Decodes a string of data which has been encoded using base-64 encoding.

    throws

    If the input string is not valid base-64 encoded data.

    example
    atob('Zm9vYmFy'); // 'foobar'
    
    since

    0.1.0

    Parameters

    • string: string

      The string to decode.

    Returns string

    The decoded string.

  • btoa(string: string): string
  • Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

    example
    btoa('foobar'); // 'Zm9vYmFy'
    btoa(''); // ''
    since

    0.1.0

    Parameters

    • string: string

      An optional string to encode. If not specified, an empty string is used.

    Returns string

    The base-64 encoded string.

  • untildify(path: string): string
  • 将波浪号路径转换为绝对路径

    remarks

    波浪号路径是指以 ~ 开头的路径,例如 ~/foo/bar

    example
    const path = untildify('~/foo/bar');
    console.log(path); // /home/username/foo/bar
    since

    0.1.0

    Parameters

    • path: string

      波浪号路径

    Returns string

    转换后的绝对路径

Type Functions

  • isDuplexStream(value: any): boolean
  • Checks if the given argument is a duplex (readable and writable) stream.

    example
    const fs = require('fs');

    isDuplexStream(new Stream.Duplex()); // true
    since

    0.1.0

    Parameters

    • value: any

      The value to check.

    Returns boolean

    If value is a duplex stream.

  • isReadableStream(value: any): boolean
  • Checks if the given argument is a readable stream.

    example
    const fs = require('fs');

    isReadableStream(fs.createReadStream('test.txt')); // true
    since

    0.1.0

    Parameters

    • value: any

      The value to check.

    Returns boolean

    If the given argument is a readable stream.

  • isStream(value: any): boolean
  • Checks if the given argument is a stream.

    example
    const fs = require('fs');

    isStream(fs.createReadStream('test.txt')); // true
    since

    0.1.0

    Parameters

    • value: any

      The value to check.

    Returns boolean

    true if the given argument is a stream, false otherwise.

  • isWritableStream(value: any): boolean
  • Checks if the given argument is a writable stream.

    example
    const fs = require('fs');

    isWritableStream(fs.createWriteStream('test.txt')); // true
    since

    0.1.0

    Parameters

    • value: any

      The value to check.

    Returns boolean

    If the given argument is a writable stream.

fs Functions

  • readFileLines(filename: PathOrFileDescriptor): string[]
  • Reads the contents of a file and splits it into lines, returns an array of lines from the specified file.

    throws

    If the file cannot be read.

    example
    /**
    * contents of test.txt :
    * line1
    * line2
    * line3
    * ___________________________
    * /
    let arr = readFileLines('test.txt');
    console.log(arr); // ['line1', 'line2', 'line3']
    since

    0.1.0

    Parameters

    • filename: PathOrFileDescriptor

      The path to the file to read.

    Returns string[]

    An array of lines from the specified file.