-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/unional/function-formatter
- Loading branch information
Showing
7 changed files
with
150 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... }`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |