Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: remove dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 5, 2018
1 parent df25217 commit 595a4fa
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 30 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
"version": "3.2.3",
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/anycli/parser/issues",
"dependencies": {
"@anycli/screen": "^0.0.3",
"lodash": "^4.17.4"
},
"dependencies": {},
"devDependencies": {
"@anycli/tslint": "^0.2.5",
"@heroku/linewrap": "^1.0.0",
"@types/chai": "^4.1.2",
"@types/lodash": "^4.14.100",
"@types/mocha": "^2.2.48",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
Expand Down
2 changes: 0 additions & 2 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// tslint:disable no-implicit-dependencies
import screen = require('@anycli/screen')
import chalk = require('chalk')

import args = require('./args')
Expand All @@ -15,7 +14,6 @@ export const deps = {
get flags(): typeof flags { return fetch('./flags') },
get parse(): typeof parse { return fetch('./parse') },
get validate(): typeof validate { return fetch('./validate') },
get screen(): typeof screen { return fetch('@anycli/screen')},
get renderList(): typeof list.renderList { return fetch('./list').renderList },
get errors(): typeof errors { return fetch('./errors') },

Expand Down
9 changes: 3 additions & 6 deletions src/help.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as _ from 'lodash'

import {deps} from './deps'
import {IFlag} from './flags'
import {sortBy} from './util'

function dim(s: string): string {
if (deps.chalk) return deps.chalk.dim(s)
Expand All @@ -25,8 +24,6 @@ export function flagUsage(flag: IFlag<any>, options: FlagUsageOptions = {}): [st

export function flagUsages(flags: IFlag<any>[], options: FlagUsageOptions = {}): [string, string | undefined][] {
if (!flags.length) return []
return _(flags)
.sortBy(f => [!f.char, f.char, f.name])
.map(f => flagUsage(f, options))
.value()
return sortBy(flags, f => [f.char ? -1 : 1, f.char, f.name])
.map(f => flagUsage(f, options))
}
11 changes: 5 additions & 6 deletions src/list.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as _ from 'lodash'

import {deps} from './deps'
import {stdtermwidth} from './screen'
import {maxBy} from './util'

function linewrap(length: number, s: string): string {
const lw = require('@heroku/linewrap')
return lw(length, deps.screen.stdtermwidth, {
return lw(length, stdtermwidth, {
skipScheme: 'ansi-color',
})(s).trim()
}
Expand All @@ -15,14 +14,14 @@ export function renderList(items: IListItem[]): string {
if (items.length === 0) {
return ''
}
const maxLength = (_.maxBy(items, '[0].length'))![0].length
const maxLength = (maxBy(items, i => i[0].length))![0].length
const lines = items.map(i => {
let left = i[0]
let right = i[1]
if (!right) {
return left
}
left = `${_.padEnd(left, maxLength)}`
left = left.padEnd(maxLength)
right = linewrap(maxLength + 2, right)
return `${left} ${right}`
})
Expand Down
5 changes: 2 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// tslint:disable interface-over-type-literal

import * as _ from 'lodash'

import {Arg} from './args'
import * as Errors from './errors'
import * as Flags from './flags'
import {pickBy} from './util'

let debug: any
try {
Expand Down Expand Up @@ -47,7 +46,7 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']
this.context = input.context || {}
this.argv = input.argv.slice(0)
this._setNames()
this.booleanFlags = _.pickBy(input.flags, f => f.type === 'boolean') as any
this.booleanFlags = pickBy(input.flags, f => f.type === 'boolean') as any
}

public parse() {
Expand Down
18 changes: 18 additions & 0 deletions src/screen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function termwidth(stream: any): number {
if (!stream.isTTY) {
return 80
}
const width = stream.getWindowSize()[0]
if (width < 1) {
return 80
}
if (width < 40) {
return 40
}
return width
}

const columns: number | null = (global as any).columns

export let stdtermwidth = columns || termwidth(process.stdout)
export let errtermwidth = columns || termwidth(process.stderr)
47 changes: 47 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export function pickBy<T>(obj: T, fn: (i: T[keyof T]) => boolean): Partial<T> {
return Object.entries(obj)
.reduce((o, [k, v]) => {
if (fn(v)) o[k] = v
return o
}, {} as any)
}

export function maxBy<T>(arr: T[], fn: (i: T) => number): T | undefined {
let max: {element: T, i: number} | undefined
for (let cur of arr) {
let i = fn(cur)
if (!max || i > max.i) {
max = {i, element: cur}
}
}
return max && max.element
}

export type SortTypes = string | number | undefined | boolean

export function sortBy<T>(arr: T[], fn: (i: T) => SortTypes | SortTypes[]): T[] {
// function castType(t: SortTypes | SortTypes[]): string | number | SortTypes[] {
// if (t === undefined) return 0
// if (t === false) return 1
// if (t === true) return -1
// return t
// }

function compare(a: SortTypes | SortTypes[], b: SortTypes | SortTypes[]): number {
a = a === undefined ? 0 : a
b = b === undefined ? 0 : b

if (Array.isArray(a) && Array.isArray(b)) {
if (a.length === 0 && b.length === 0) return 0
let diff = compare(a[0], b[0])
if (diff !== 0) return diff
return compare(a.slice(1), b.slice(1))
}

if (a < b) return -1
if (a > b) return 1
return 0
}

return arr.sort((a, b) => compare(fn(a), fn(b)))
}
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# yarn lockfile v1


"@anycli/screen@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@anycli/screen/-/screen-0.0.3.tgz#f0afd970c3ed725702948a45a874ede1fdd9362e"

"@anycli/tslint@^0.2.5":
version "0.2.5"
resolved "https://registry.yarnpkg.com/@anycli/tslint/-/tslint-0.2.5.tgz#63feeb981b11f36326e0cb745c62f51d55c2ed67"
Expand All @@ -21,10 +17,6 @@
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.1.2.tgz#f1af664769cfb50af805431c407425ed619daa21"

"@types/lodash@^4.14.100":
version "4.14.100"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.100.tgz#f353dd9d3a9785638b6cb8023e6639097bd31969"

"@types/mocha@^2.2.48":
version "2.2.48"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.48.tgz#3523b126a0b049482e1c3c11877460f76622ffab"
Expand Down

0 comments on commit 595a4fa

Please sign in to comment.