<commit_before>import util from 'util';
import _ from 'lodash';
import php from 'locutus/php';

export const logger = {};
logger.log = console.log.bind(console);
logger.trace = console.trace.bind(console);
logger.info = console.info.bind(console);
logger.warn = console.warn.bind(console);
logger.error = console.error.bind(console);

/**
 * @param any
 * @returns {Array}
 */
export function castArray(any) {
  any = !_.isUndefined(any) && !_.isNull(any) ? any : [];
  any = _.isArray(any) ? any : [any];

  return any;
}

/**
 * @param object
 * @param {object} [opts]
 * @returns {string}
 */
export function inspect(
  object,
  opts = {
    depth: null,
    colors: true,
    maxArrayLength: 1000,
  },
) {
  return util.inspect(object, opts);
}

/**
 * @param any
 * @returns {boolean}
 */
export function isStringifyedNumber(any) {
  const int = _.toInteger(any);

  if (int === 0 && any !== '0') {
    return false;
  }

  return any == int;
}
/**
 * @param something
 * @param [encoding='utf-8']
 * @returns {Buffer|*}
 */
export function toBuffer(something, encoding = 'utf-8') {
  let retVal = something;

  if (_.isArray(something)) {
    retVal = _.map(something, item => toBuffer(item, encoding));
  } else if (Buffer.isBuffer(something)) {
    retVal = something;
  } else if (_.isString(something)) {
    retVal = Buffer.from(something, encoding);
  } else if (_.isPlainObject(something)) {
    const obj = _.clone(something);
    _.forEach(obj, (val, key) => (obj[key] = toBuffer(val, encoding)));

    retVal = obj;
  }

  return retVal;
}

/**
 * @param something
 * @param {String} [encoding='utf8']
 * @returns {string|*}
 */
export function buffer2str(something, encoding = 'utf8') {
  return Buffer.isBuffer(something) ? something.toString(encoding) : something;
}

/**
 * @param something
 * @returns {Array}
 */
export function str2ascii(something) {
  const retVal = [];
  const buffer = !Buffer.isBuffer(something) ? Buffer.from(something, 'binary') : something;

  for (let i = 0, length = buffer.length; i < length; i++) {
    retVal.push(buffer[i]);
  }

  return retVal;
}

/**
 * @param something
 * @returns {String}
 */
export function str2hex(something) {
  const retVal = !Buffer.isBuffer(something) ? Buffer.from(something, 'binary') : something;

  return retVal.toString('hex');
}

export function clone(instance) {
  return _.merge({}, Object.create(Object.getPrototypeOf(instance)), instance);
}

// php.info.ini_set('unicode.semantics', 'on');
php.info.ini_set('locutus.objectsAsArrays', false);

/**
 * @param {string} format
 * @param {Buffer} buffer
 * @returns {Object|Object[]}
 */
php.unpack = function unpack(format, buffer) {
  /**
   * Параметр format задается в виде строки и состоит из кодов формата и
   * опционального аргумента повторения. Аргумент может быть целочисленным,<commit_msg>ini_set 'objectsAsArrays' to default (true)<commit_after>import util from 'util';
import _ from 'lodash';
import php from 'locutus/php';

export const logger = {};
logger.log = console.log.bind(console);
logger.trace = console.trace.bind(console);
logger.info = console.info.bind(console);
logger.warn = console.warn.bind(console);
logger.error = console.error.bind(console);

/**
 * @param any
 * @returns {Array}
 */
export function castArray(any) {
  any = !_.isUndefined(any) && !_.isNull(any) ? any : [];
  any = _.isArray(any) ? any : [any];

  return any;
}

/**
 * @param object
 * @param {object} [opts]
 * @returns {string}
 */
export function inspect(
  object,
  opts = {
    depth: null,
    colors: true,
    maxArrayLength: 1000,
  },
) {
  return util.inspect(object, opts);
}

/**
 * @param any
 * @returns {boolean}
 */
export function isStringifyedNumber(any) {
  const int = _.toInteger(any);

  if (int === 0 && any !== '0') {
    return false;
  }

  return any == int;
}
/**
 * @param something
 * @param [encoding='utf-8']
 * @returns {Buffer|*}
 */
export function toBuffer(something, encoding = 'utf-8') {
  let retVal = something;

  if (_.isArray(something)) {
    retVal = _.map(something, item => toBuffer(item, encoding));
  } else if (Buffer.isBuffer(something)) {
    retVal = something;
  } else if (_.isString(something)) {
    retVal = Buffer.from(something, encoding);
  } else if (_.isPlainObject(something)) {
    const obj = _.clone(something);
    _.forEach(obj, (val, key) => (obj[key] = toBuffer(val, encoding)));

    retVal = obj;
  }

  return retVal;
}

/**
 * @param something
 * @param {String} [encoding='utf8']
 * @returns {string|*}
 */
export function buffer2str(something, encoding = 'utf8') {
  return Buffer.isBuffer(something) ? something.toString(encoding) : something;
}

/**
 * @param something
 * @returns {Array}
 */
export function str2ascii(something) {
  const retVal = [];
  const buffer = !Buffer.isBuffer(something) ? Buffer.from(something, 'binary') : something;

  for (let i = 0, length = buffer.length; i < length; i++) {
    retVal.push(buffer[i]);
  }

  return retVal;
}

/**
 * @param something
 * @returns {String}
 */
export function str2hex(something) {
  const retVal = !Buffer.isBuffer(something) ? Buffer.from(something, 'binary') : something;

  return retVal.toString('hex');
}

export function clone(instance) {
  return _.merge({}, Object.create(Object.getPrototypeOf(instance)), instance);
}

// php.info.ini_set('unicode.semantics', 'on');
// php.info.ini_set('locutus.objectsAsArrays', false);

/**
 * @param {string} format
 * @param {Buffer} buffer
 * @returns {Object|Object[]}
 */
php.unpack = function unpack(format, buffer) {
  /**
   * Параметр format задается в виде строки и состоит из кодов формата и
   * опционального аргумента повторения. Аргумент может быть целочисленным,
{'fraction_non_alphanumeric': 0.16652090257127863, 'fraction_numerical': 0.004897673605037607, 'mean_word_length': 3.9721739130434783, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7204889', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1925, 'n_words': 463}