<commit_before>#!/usr/bin/env node

// warning: hacky code below
"use strict"

var args = process.argv.slice(2)
var vm = require('vm')
var debug = require('debug')('nimp')

var highlight = require('ansi-highlight')
var inspect = require('util').inspect
global.require = require
var code = args.join(' ')
if (!code) {
  code = 'global.' // default = list global properties
}

code = code.trim()
code = code.replace(/ /gmi, '.prototype.') // Function protoMethod syntax.
var input = code
var parts = code.split('.')
parts[0] = [
  '(function get(part0) {',
    'if (typeof global[part0] !== "undefined") return global[part0]',
    'return require(part0)',
  '})("'+parts[0]+'")'
].join('\n')
code = parts.join('.')

// array of arrays of keys. walks up proto chain
function protokeys(obj) {
  if (obj === global) loadBuiltIns()
  var keys = [];
  while (obj) {
    keys.push(Object.getOwnPropertyNames(obj));
    obj = Object.getPrototypeOf(obj);
  }
  return keys;
}

try {
  var listKeys = code[code.length - 1] === '.'
  if (listKeys) {
    code = code.replace(/\.+$/m, '') // remove trailing .
    input = input.replace(/\.+$/m, '')
  }

  vm.runInThisContext('global.$$item = ' + code + ';')
  var item = $$item
  delete global.$$item

  if (listKeys) return console.info(renderProtoKeys(input, protokeys(item)))

  console.info(highlight(getString(item)))

} catch(err) {
  console.error(err.message)
  debug('\n', highlight(code))
  process.exit(1)
}

function getString(item) {
  var str = (typeof item === "function")
  ?  item.toString()
  : inspect(item, {depth: 30}) // 30 depth should be enough for anyone!
  return str
}

function loadBuiltIns() {<commit_msg>Pseudo fix to load local node_modules<commit_after>#!/usr/bin/env node

// warning: hacky code below
"use strict"

var args = process.argv.slice(2)
var vm = require('vm')
var debug = require('debug')('nim')
var join = require('path').join

var highlight = require('ansi-highlight')
var inspect = require('util').inspect
global.require = require
var code = args.join(' ')
if (!code) {
  code = 'global.' // default = list global properties
}

code = code.trim()
code = code.replace(/ /gmi, '.prototype.') // Function protoMethod syntax.
var input = code
var parts = code.split('.')
parts[0] = [
  '(function get(part0) {',
    'if (typeof global[part0] !== "undefined") return global[part0]',
    'return require(part0)',
  '})("'+parts[0]+'")'
].join('\n')
code = parts.join('.')

// array of arrays of keys. walks up proto chain
function protokeys(obj) {
  if (obj === global) loadBuiltIns()
  var keys = [];
  while (obj) {
    keys.push(Object.getOwnPropertyNames(obj));
    obj = Object.getPrototypeOf(obj);
  }
  return keys;
}

try {
  var listKeys = code[code.length - 1] === '.'
  if (listKeys) {
    code = code.replace(/\.+$/m, '') // remove trailing .
    input = input.replace(/\.+$/m, '')
  }
  module.paths.push(join(process.cwd(), 'node_modules'))
  vm.runInThisContext('global.$$item = ' + code + ';')
  module.paths.pop()
  var item = $$item
  delete global.$$item

  if (listKeys) return console.info(renderProtoKeys(input, protokeys(item)))

  console.info(highlight(getString(item)))

} catch(err) {
  console.error(err.message)
  debug('\n', highlight(code))
  process.exit(1)
}

function getString(item) {
  var str = (typeof item === "function")
  ?  item.toString()
  : inspect(item, {depth: 30}) // 30 depth should be enough for anyone!
  return str
}

function loadBuiltIns() {
{'fraction_non_alphanumeric': 0.1871042026482441, 'fraction_numerical': 0.007484168105929764, 'mean_word_length': 3.7997237569060776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5538666', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1173, 'n_words': 297}