All files / src/node options.ts

100% Statements 69/69
100% Branches 15/15
100% Functions 12/12
100% Lines 60/60

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134  66x 66x   66x     66x         66x 281x 279x     66x   66x   1184x   296x 296x   128x 128x   167x 167x   1x       295x   294x     66x 1144x     66x 506x 280x     66x       66x 66x   66x       66x 23x     66x 66x   66x     66x   66x         66x 66x       66x         66x 66x   66x         66x 66x   66x     66x 837x 66x     66x 465x   66x     66x 9x 66x     66x 3x       66x 66x 66x   66x         66x  
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 opendirDefaults: opts.IOpendirOptions = {
  encoding: 'utf8',
  bufferSize: 32,
  recursive: false,
};
export const getOpendirOptions = optsGenerator<opts.IOpendirOptions>(opendirDefaults);
export const getOpendirOptsAndCb = optsAndCbGenerator<opts.IOpendirOptions, misc.IDir>(getOpendirOptions);
 
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 statfsDefaults: opts.IStafsOptions = {
  bigint: false,
};
export const getStatfsOptions: (options?: any) => opts.IStafsOptions = (options = {}) =>
  Object.assign({}, statfsDefaults, options);
export const getStatfsOptsAndCb: (
  options: any,
  callback?: misc.TCallback<misc.IStatFs>,
) => [opts.IStafsOptions, misc.TCallback<misc.IStatFs>] = (options, callback?) =>
  typeof options === 'function'
    ? [getStatfsOptions(), options]
    : [getStatfsOptions(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);