Skip to content

Commit

Permalink
fix: property with dashes
Browse files Browse the repository at this point in the history
`{ 'a-b': 1 }`
  • Loading branch information
unional committed Oct 5, 2020
1 parent 2be9ac6 commit 56c5e8a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
47 changes: 34 additions & 13 deletions src/tersify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { tersible } from './tersible';
import { tersify } from './tersify';
import { tersible } from './tersible'
import { tersify } from './tersify'

describe('undefined', () => {
test(`tersify(undefined)`, () => {
Expand Down Expand Up @@ -592,9 +592,9 @@ describe('function', () => {

test('with variable declaration', () => {
const subject: any = function () {
const date = new Date('2020-05-14T11:45:27.234Z');
return date;
};
const date = new Date('2020-05-14T11:45:27.234Z')
return date
}
expect(tersify(subject)).toBe(`fn() { const date = 2020-05-14T11:45:27.234Z; return date }`)
expect(tersify(subject, { maxLength: 59 })).toBe(`fn() { const date = 2020-05-14T11:45:27.234Z; return date }`)
expect(tersify(subject, { maxLength: 58 })).toBe(`fn() { const date = 2020-05-14T11:45:27.234Z; return ... }`)
Expand Down Expand Up @@ -761,7 +761,7 @@ describe('function', () => {
test('with for loop with break', () => {
expect(tersify(function () {
for (; ;) {
break;
break
}
})).toBe('fn() { for (;;) { break } }')
})
Expand Down Expand Up @@ -924,7 +924,7 @@ describe('function', () => {
test('create namespaced class', () => {
function fool() {
// @ts-ignore
return new ns.Foo();
return new ns.Foo()
}

expect(tersify(fool)).toBe(`fn fool() { return new ns.Foo() }`)
Expand All @@ -943,11 +943,19 @@ describe('function', () => {
})

test('returns tagged literal', () => {
function tag(...args: any[]) {}
function tag(...args: any[]) { }

const subject = function templateLiteral() { return tag`a${123456789}c` }
expect(tersify(subject)).toBe('fn templateLiteral() { return tag`a${123456789}c` }')
})

test('with comment', () => {
function withComment() {
// comment
}

expect(tersify(withComment)).toBe('fn withComment() {}')
})
})

describe('arrow function', () => {
Expand Down Expand Up @@ -1229,9 +1237,9 @@ describe('arrow function', () => {

test('with variable declaration', () => {
const subject: any = () => {
const date = new Date('2020-05-14T11:45:27.234Z');
return date;
};
const date = new Date('2020-05-14T11:45:27.234Z')
return date
}
expect(tersify(subject)).toBe(`() => { const date = 2020-05-14T11:45:27.234Z; return date }`)
expect(tersify(subject, { maxLength: 60 })).toBe(`() => { const date = 2020-05-14T11:45:27.234Z; return date }`)
expect(tersify(subject, { maxLength: 59 })).toBe(`() => { const date = 2020-05-14T11:45:27.234Z; return ... }`)
Expand Down Expand Up @@ -1398,7 +1406,7 @@ describe('arrow function', () => {
test('with for loop with break', () => {
expect(tersify(() => {
for (; ;) {
break;
break
}
})).toBe('() => { for (;;) { break } }')
})
Expand Down Expand Up @@ -1582,7 +1590,9 @@ describe('object', () => {
expect(tersify({ get x() { return 1 } }, { maxLength: 5 })).toBe('{ . }')
expect(tersify({ get x() { return 1 } }, { maxLength: 4 })).toBe('{ ..')
})

test('getter with dash', () => {
expect(tersify({ get 'a-b'() { return 1 } })).toBe(`{ 'a-b': [Get] }`)
})
test('object with setter', () => {
expect(tersify({ set x(_) { } })).toBe('{ x: [Set] }')
expect(tersify({ set x(_) { } }, { maxLength: 12 })).toBe('{ x: [Set] }')
Expand Down Expand Up @@ -1630,6 +1640,10 @@ describe('object', () => {
expect(tersify({ a: /abcd/gi })).toBe('{ a: /abcd/gi }')
})

test('property with dash', () => {
expect(tersify({ 'a-b': 1 })).toBe(`{ 'a-b': 1 }`)
})

test('date object property', () => {
expect(tersify({ a: new Date('2020-05-14T11:45:27.234Z') })).toBe(`{ a: 2020-05-14T11:45:27.234Z }`)
})
Expand Down Expand Up @@ -1678,6 +1692,10 @@ describe('object', () => {
expect(tersify({ a: function () { } }, { maxLength: 9 })).toBe('{ a(... }')
})

test('anomyous function with dash', () => {
expect(tersify({ 'a-b': function () { } })).toBe(`{ 'a-b'() {} }`)
})

test('named function', () => {
expect(tersify({ a: function foo() { } })).toBe('{ a() {} }')
})
Expand Down Expand Up @@ -1710,6 +1728,9 @@ describe('object', () => {
test('arrow function', () => {
expect(tersify({ a: () => { } })).toBe('{ a: () => {} }')
})
test('arrow function with dash', () => {
expect(tersify({ 'a-b': () => { } })).toBe(`{ 'a-b': () => {} }`)
})

test('trimming object with arrow function', () => {
expect(tersify({ a: () => false, c: { b: 1, c: 'c' }, d: true }, { maxLength: 20 })).toBe(`{ a: () => fals... }`)
Expand Down
13 changes: 9 additions & 4 deletions src/tersify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function tersifyInstance(context: TersifyContext, value: object, length: number)
trim(context, `${nameStr} { }`, length) :
`${nameStr} { ${trimmedContent} }`
}

function getPropStr(context: TersifyContext, bag: Record<any, any>, key: string, length: number, isLastKey: boolean) {
const commaAndSpaceLen = isLastKey ? 0 : 2
const colonAndSpaceLen = 2
Expand All @@ -224,7 +225,7 @@ function getPropStr(context: TersifyContext, bag: Record<any, any>, key: string,
if (d.get) accessor.push('Get')
if (d.set) accessor.push('Set')
if (accessor.length > 0) {
return `${key}: [${accessor.join('/')}]`
return `${getPropKey(key)}: [${accessor.join('/')}]`
}

const value = bag[key]
Expand All @@ -235,7 +236,7 @@ function getPropStr(context: TersifyContext, bag: Record<any, any>, key: string,
{ ...context, path },
value,
length - key.length - commaAndSpaceLen - colonAndSpaceLen)
return `${key}: ${propValue}`
return `${getPropKey(key)}: ${propValue}`
}
else {
const propValue = tersifyValue(
Expand All @@ -244,7 +245,7 @@ function getPropStr(context: TersifyContext, bag: Record<any, any>, key: string,
length - key.length - commaAndSpaceLen + fnLen)
const asyncToken = /^async fn/.test(propValue) ? 'async ' : ''
const generatorToken = /^(async )?fn\*/.test(propValue) ? '*' : ''
return `${asyncToken}${generatorToken}${key}${propValue.slice(propValue.indexOf('('))}`
return `${asyncToken}${generatorToken}${getPropKey(key)}${propValue.slice(propValue.indexOf('('))}`
}
}
else {
Expand All @@ -253,6 +254,10 @@ function getPropStr(context: TersifyContext, bag: Record<any, any>, key: string,
{ ...context, path, noTrim: true },
value,
length - key.length - colonAndSpaceLen - commaAndSpaceLen)
return `${key}: ${propValue}`
return `${getPropKey(key)}: ${propValue}`
}
}

function getPropKey(key: string) {
return key.indexOf('-') >=0 ? `'${key}'` : key
}

0 comments on commit 56c5e8a

Please sign in to comment.