<commit_before>#!/usr/bin/env node
'use strict'

const program = require('commander');
const miteApi = require('mite-api');
const chalk = require('chalk');
const tableLib = require('table')
const table = tableLib.table;

const pkg = require('./../package.json');
const config = require('./config.js');
const formater = require('./lib/formater');
const BUDGET_TYPE = formater.BUDGET_TYPE;
const SORT_OPTIONS = [
  'id',
  'name',
  'updated_at',
  'created_at',
  'hourly_rate'
];
const SORT_OPTIONS_DEFAULT = 'name';

program
  .version(pkg.version)
  .description('list, filter & search for servuces')
  .option(
    '--search <query>',
    'optional search string which must be somewhere in the services’ name ' +
    '(case insensitive)'
  )
  .option(
    '-a, --archived',
    'When used will only return archived service which are not returned when ' +
    'not used.',
    false
  )
  .option(
    '--sort <column>',
    `optional column the results should be case-insensitive ordered by `+
    `(default: "${SORT_OPTIONS_DEFAULT}"), ` +
    `valid values: ${SORT_OPTIONS.join(', ')}`,
    (value) => {
      if (SORT_OPTIONS.indexOf(value) === -1) {
        console.error(
          'Invalid value for sort option: "%s", valid values are: ',
          value,
          SORT_OPTIONS.join(', ')
        );
        process.exit(2);
      }
      return value;
    },
    SORT_OPTIONS_DEFAULT // default sor
  )
  .option(
    '--billable <true|false>',
    'wheter to show only billable or not-billable entries, no filter is used ' +
    'when argument is not used',
    ((val) => {
      if (typeof val !== 'string') {
        return val;
      }
      return ['true', 'yes', 'ja', 'ok', '1'].indexOf(val.toLowerCase()) > -1;
    }),
    null
  )
  .parse(process.argv);

const opts = {
  limit: 1000
};
if (program.search) {
  opts.name = program.search;
}

let method = 'getServices';
if (program.archived) {
  method = 'getArchivedServices';
}

const mite = miteApi(config.get());
mite[method](opts, (err, responseData) => {
  if (err) {
    throw err;
  }

  const tableData = responseData.map((v) => v.service)
    .filter((a) => {
      if (program.billable === null) {
        return true;
      }
      return program.billable === a.billable;
    })
    .sort((a, b) => {
      if (!program.sort) {
        return 0;
      }
      const sortByAttributeName = program.sort;
      var val1 = String(a[sortByAttributeName]).toLowerCase();
      var val2 = String(b[sortByAttributeName]).toLowerCase();
      if (val1 > val2) {
        return 1;
      } else if (val1 < val2) {
        return -1;
      } else {
        return 0;
      }
    })
    .map((service) => {
      return [
        service.id,
        service.name,
        service.billable ? 'yes' : 'no',
        formater.budget(BUDGET_TYPE.CENTS, service.hourly_rate),
        service.note.replace(/\r?\n/g, ' '),
      ];
    });
  tableData.unshift([
    'id',
    'name',
    'billable',
    'rate',
    'note',
  ].map(v=> chalk.bold(v)));
  const tableConfig = {<commit_msg>fix(service): rate displayed as "-" when null<commit_after>#!/usr/bin/env node
'use strict'

const program = require('commander');
const miteApi = require('mite-api');
const chalk = require('chalk');
const tableLib = require('table')
const table = tableLib.table;

const pkg = require('./../package.json');
const config = require('./config.js');
const formater = require('./lib/formater');
const BUDGET_TYPE = formater.BUDGET_TYPE;
const SORT_OPTIONS = [
  'id',
  'name',
  'updated_at',
  'created_at',
  'hourly_rate',
  'rate' // alias for hourly_rate
];
const SORT_OPTIONS_DEFAULT = 'name';

program
  .version(pkg.version)
  .description('list, filter & search for servuces')
  .option(
    '--search <query>',
    'optional search string which must be somewhere in the services’ name ' +
    '(case insensitive)'
  )
  .option(
    '-a, --archived',
    'When used will only return archived service which are not returned when ' +
    'not used.',
    false
  )
  .option(
    '--sort <column>',
    `optional column the results should be case-insensitive ordered by `+
    `(default: "${SORT_OPTIONS_DEFAULT}"), ` +
    `valid values: ${SORT_OPTIONS.join(', ')}`,
    (value) => {
      if (SORT_OPTIONS.indexOf(value) === -1) {
        console.error(
          'Invalid value for sort option: "%s", valid values are: ',
          value,
          SORT_OPTIONS.join(', ')
        );
        process.exit(2);
      }
      return value;
    },
    SORT_OPTIONS_DEFAULT // default sort
  )
  .option(
    '--billable <true|false>',
    'wheter to show only billable or not-billable entries, no filter is used ' +
    'when argument is not used',
    ((val) => {
      if (typeof val !== 'string') {
        return val;
      }
      return ['true', 'yes', 'ja', 'ok', '1'].indexOf(val.toLowerCase()) > -1;
    }),
    null
  )
  .parse(process.argv);

const opts = {
  limit: 1000
};
if (program.search) {
  opts.name = program.search;
}

let method = 'getServices';
if (program.archived) {
  method = 'getArchivedServices';
}

const mite = miteApi(config.get());
mite[method](opts, (err, responseData) => {
  if (err) {
    throw err;
  }

  const tableData = responseData.map((v) => v.service)
    .filter((a) => {
      if (program.billable === null) {
        return true;
      }
      return program.billable === a.billable;
    })
    .sort((a, b) => {
      if (!program.sort) {
        return 0;
      }
      let sortByAttributeName = program.sort;
      if (sortByAttributeName === 'rate') {
        sortByAttributeName = 'hourly_rate';
      }
      var val1 = String(a[sortByAttributeName]).toLowerCase();
      var val2 = String(b[sortByAttributeName]).toLowerCase();
      if (val1 > val2) {
        return 1;
      } else if (val1 < val2) {
        return -1;
      } else {
        return 0;
      }
    })
    .map((service) => {
      let rate = formater.budget(BUDGET_TYPE.CENTS, service.hourly_rate);
      if (!service.hourly_rate) {
        rate = '-';
      }
      return [
        service.id,
        service.name,
        service.billable ? 'yes' : 'no',
        rate,
        service.note.replace(/\r?\n/g, ' '),
      ];
    });
  tableData.unshift([
    'id',
    'name',
    'billable',
    'rate',
    'note',
  ].map(v=> chalk.bold(v)));
  const tableConfig = {
{'fraction_non_alphanumeric': 0.1638853904282116, 'fraction_numerical': 0.005667506297229219, 'mean_word_length': 2.484914975315414, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '1038136', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2126, 'n_words': 508}