All files / src/node options.ts

100% Statements 59/59
100% Branches 12/12
100% Functions 10/10
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113  45x 45x   45x     45x         45x 649x 648x     45x   45x   2910x   194x 194x   67x 67x   126x 126x   1x       193x   192x     45x 2888x     45x 935x 882x     45x       45x 45x   45x       45x 128x     45x 45x   45x     45x   45x         45x 45x       45x         45x 45x   45x     45x 9385x 45x     45x 9174x   45x 45x 45x   45x         45x  
import type * as opts from './types/options';
import { FLAGS, MODE } from './constants';
import { assertEncoding } from '../encoding';
import * as misc from './types/misc';
import { validateCallback } from './util';
import { IAppendFileOptions } from '../volume';
 
const mkdirDefaults: opts.IMkdirOptions = {
  mode: MODE.DIR,
  recursive: false,
};
 
export const getMkdirOptions = (options): opts.IMkdirOptions => {
  if (typeof options === 'number') return Object.assign({}, mkdirDefaults, { mode: options });
  return Object.assign({}, mkdirDefaults, options);
};
 
const ERRSTR_OPTS = tipeof => `Expected options to be either an object or a string, but got ${tipeof} instead`;
 
export function getOptions<T extends opts.IOptions>(defaults: T, options?: T | string): T {
  let opts: T;
  if (!options) return defaults;
  else {
    const tipeof = typeof options;
    switch (tipeof) {
      case 'string':
        opts = Object.assign({}, defaults, { encoding: options as string });
        break;
      case 'object':
        opts = Object.assign({}, defaults, options);
        break;
      default:
        throw TypeError(ERRSTR_OPTS(tipeof));
    }
  }
 
  if (opts.encoding !== 'buffer') assertEncoding(opts.encoding);
 
  return opts;
}
 
export function optsGenerator<TOpts>(defaults: TOpts): (opts) => TOpts {
  return options => getOptions(defaults, options);
}
 
export function optsAndCbGenerator<TOpts, TResult>(getOpts): (options, callback?) => [TOpts, misc.TCallback<TResult>] {
  return (options, callback?) =>
    typeof options === 'function' ? [getOpts(), options] : [getOpts(options), validateCallback(callback)];
}
 
export const optsDefaults: opts.IOptions = {
  encoding: 'utf8',
};
 
export const getDefaultOpts = optsGenerator<opts.IOptions>(optsDefaults);
export const getDefaultOptsAndCb = optsAndCbGenerator<opts.IOptions, any>(getDefaultOpts);
 
const rmdirDefaults: opts.IRmdirOptions = {
  recursive: false,
};
 
export const getRmdirOptions = (options): opts.IRmdirOptions => {
  return Object.assign({}, rmdirDefaults, options);
};
 
const getRmOpts = optsGenerator<opts.IOptions>(optsDefaults);
export const getRmOptsAndCb = optsAndCbGenerator<opts.IRmOptions, any>(getRmOpts);
 
const readFileOptsDefaults: opts.IReadFileOptions = {
  flag: 'r',
};
export const getReadFileOptions = optsGenerator<opts.IReadFileOptions>(readFileOptsDefaults);
 
const readdirDefaults: opts.IReaddirOptions = {
  encoding: 'utf8',
  recursive: false,
  withFileTypes: false,
};
export const getReaddirOptions = optsGenerator<opts.IReaddirOptions>(readdirDefaults);
export const getReaddirOptsAndCb = optsAndCbGenerator<opts.IReaddirOptions, misc.TDataOut[] | misc.IDirent[]>(
  getReaddirOptions,
);
 
const appendFileDefaults: opts.IAppendFileOptions = {
  encoding: 'utf8',
  mode: MODE.DEFAULT,
  flag: FLAGS[FLAGS.a],
};
export const getAppendFileOpts = optsGenerator<IAppendFileOptions>(appendFileDefaults);
export const getAppendFileOptsAndCb = optsAndCbGenerator<IAppendFileOptions, void>(getAppendFileOpts);
 
const statDefaults: opts.IStatOptions = {
  bigint: false,
};
export const getStatOptions: (options?: any) => opts.IStatOptions = (options = {}) =>
  Object.assign({}, statDefaults, options);
export const getStatOptsAndCb: (
  options: any,
  callback?: misc.TCallback<misc.IStats>,
) => [opts.IStatOptions, misc.TCallback<misc.IStats>] = (options, callback?) =>
  typeof options === 'function' ? [getStatOptions(), options] : [getStatOptions(options), validateCallback(callback)];
 
const realpathDefaults: opts.IReadFileOptions = optsDefaults;
export const getRealpathOptions = optsGenerator<opts.IRealpathOptions>(realpathDefaults);
export const getRealpathOptsAndCb = optsAndCbGenerator<opts.IRealpathOptions, misc.TDataOut>(getRealpathOptions);
 
export const writeFileDefaults: opts.IWriteFileOptions = {
  encoding: 'utf8',
  mode: MODE.DEFAULT,
  flag: FLAGS[FLAGS.w],
};
export const getWriteFileOptions = optsGenerator<opts.IWriteFileOptions>(writeFileDefaults);