Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Nov 26, 2017
2 parents 1dd3750 + 6b39a99 commit 015f2f0
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 63 deletions.
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@
"tslint": "^5.8.0",
"tslint-config-unional": "^0.8.0",
"typescript": "^2.6.1"
},
"dependencies": {
"stringify-object": "^3.2.1"
}
}
3 changes: 2 additions & 1 deletion src/index.spec.ts → src/formatFunction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava'
import { formatFunction } from './index';

import { formatFunction } from './index'

test('anonymous function', t => {
t.is(formatFunction(function () { }), 'function () {}')
Expand Down
60 changes: 60 additions & 0 deletions src/formatFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os = require('os')

export function formatFunction(fn: Function, option = { maxLength: 120 }) {
const str = fn.toString()
if (isArrow(str))
return formatArrow(str, option.maxLength)
else
return formatFn(str, option.maxLength)
}

function isArrow(str: string) {
const lines = str.split(os.EOL)

// https://regex101.com/r/0HtLzb/1
return /[\(]?.*[\)]? =>/.test(lines[0])
}

function formatArrow(str: string, maxLength) {
const lines = str.split(os.EOL)
const trimmedlines = lines.map(l => l.trim())
let singleLine = trimmedlines.join(' ');

// https://regex101.com/r/1Nv7hN/2
const matchSingleExpression = /=> { return (.*); }/.exec(singleLine)
if (matchSingleExpression) {
const singleExpression = matchSingleExpression[1]
singleLine = lines[0].slice(0, lines[0].length - 1) + singleExpression
}

if (singleLine.length > maxLength) {
singleLine = trimWithBracket(singleLine, maxLength)
}
if (singleLine.length > maxLength) {
// after trimming it is still too long
singleLine = singleLine.slice(0, maxLength - 3) + '...'
}
return singleLine
}

function trimWithBracket(singleLine, maxLength) {
// https://regex101.com/r/HrkxfW/1
const parts = /(.* { )(.*)( })/.exec(singleLine)
return parts ? parts[1] + parts[2].slice(0, maxLength - parts[1].length - parts[3].length - 3) + '...' + parts[3] : singleLine
}

function formatFn(str: string, maxLength) {
const lines = str.split(os.EOL)
const trimmedlines = lines.map(l => l.trim())
let singleLine = trimmedlines.join(' ');

if (singleLine.length > maxLength) {
singleLine = trimWithBracket(singleLine, maxLength)
}

if (singleLine.length > maxLength) {
// after trimming it is still too long
singleLine = singleLine.slice(0, maxLength - 3) + '...'
}
return singleLine
}
42 changes: 42 additions & 0 deletions src/formatObject.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { test } from 'ava'

import { formatObject } from './index'

test('empty object', t => {
t.is(formatObject({}), '{}')
})

test('simple object', t => {
t.is(formatObject({ a: 1 }), '{ a: 1 }')
})

test('complex object', t => {
t.is(formatObject({ a: { b: 1, c: 'c' }, d: true }), `{ a: { b: 1, c: 'c' }, d: true }`)
})

test('object with function', t => {
t.is(formatObject({ a: () => { return true } }), `{ a: () => true }`)
})

test('long object', t => {
t.is(formatObject({ a: { b: 1, c: 'c' }, d: true }, { maxLength: 20 }), `{ a: { b: 1, c:... }`)
})

test('long object with function', t => {
t.is(formatObject({ a: () => false, c: { b: 1, c: 'c' }, d: true }, { maxLength: 20 }), `{ a: () => fals... }`)
})

test('object with long function', t => {
t.is(formatObject({
a: function (x, y) {
console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)
console.log(6)
x++
return y
}, c: { b: 1, c: 'c' }, d: true
}, { maxLength: 100 }), `{ a: function (x, y) { console.log(1); console.log(2); console.log(3); console.log(4); console... }`)
})
20 changes: 20 additions & 0 deletions src/formatObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import stringifyObject = require('stringify-object');

import { formatFunction } from './formatFunction'

export function formatObject(obj, option = { maxLength: 120 }) {
let str: string = stringifyObject(obj, {
indent: ' ',
inlineCharacterLimit: Infinity,
transform: (obj, prop, originalResult) => {
if (typeof obj[prop] === 'function')
return formatFunction(obj[prop], { maxLength: Infinity })
return originalResult
}
})
str = str.replace(/\{/g, '{ ').replace(/\}/g, ' }').replace(/{\s{2}}/g, '{}')
if (str.length > option.maxLength) {
str = str.slice(0, option.maxLength - 5) + '...' + str.slice(-2)
}
return str
}
62 changes: 2 additions & 60 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,2 @@
import os = require('os')

export function formatFunction(fn: Function, option = { maxLength: 120 }) {
const str = fn.toString()
if (isArrow(str))
return formatArrow(str, option.maxLength)
else
return formatFn(str, option.maxLength)
}

function isArrow(str: string) {
const lines = str.split(os.EOL)

// https://regex101.com/r/0HtLzb/1
return /[\(]?.*[\)]? =>/.test(lines[0])
}

function formatArrow(str: string, maxLength) {
const lines = str.split(os.EOL)
const trimmedlines = lines.map(l => l.trim())
let singleLine = trimmedlines.join(' ');

// https://regex101.com/r/1Nv7hN/2
const matchSingleExpression = /=> { return (.*); }/.exec(singleLine)
if (matchSingleExpression) {
const singleExpression = matchSingleExpression[1]
singleLine = lines[0].slice(0, lines[0].length - 1) + singleExpression
}

if (singleLine.length > maxLength) {
singleLine = trimWithBracket(singleLine, maxLength)
}
if (singleLine.length > maxLength) {
// after trimming it is still too long
singleLine = singleLine.slice(0, maxLength - 3) + '...'
}
return singleLine
}

function trimWithBracket(singleLine, maxLength) {
// https://regex101.com/r/HrkxfW/1
const parts = /(.* { )(.*)( })/.exec(singleLine)
return parts ? parts[1] + parts[2].slice(0, maxLength - parts[1].length - parts[3].length - 3) + '...' + parts[3] : singleLine
}

function formatFn(str: string, maxLength) {
const lines = str.split(os.EOL)
const trimmedlines = lines.map(l => l.trim())
let singleLine = trimmedlines.join(' ');

if (singleLine.length > maxLength) {
singleLine = trimWithBracket(singleLine, maxLength)
}

if (singleLine.length > maxLength) {
// after trimming it is still too long
singleLine = singleLine.slice(0, maxLength - 3) + '...'
}
return singleLine
}
export * from './formatFunction'
export * from './formatObject'

0 comments on commit 015f2f0

Please sign in to comment.