Skip to content

Commit

Permalink
Merge pull request #35 from j2inn/hnum/toLocaleString
Browse files Browse the repository at this point in the history
Adds locale specific formatting to HNum.toString
  • Loading branch information
thoughtpalette authored Jul 17, 2024
2 parents 29425db + ba50584 commit be5e721
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion spec/core/HDict.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('HDict', function (): void {
it('returns a human readable string', function (): void {
dict.set('nullVal', null)
expect(dict.toString()).toBe(
'{foo: foovalue, goo: 99.0, soo, nullVal: null}'
'{foo: foovalue, goo: 99, soo, nullVal: null}'
)
})
}) // #toString()
Expand Down
2 changes: 1 addition & 1 deletion spec/core/HGrid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ type,val
it('returns a human readable string', function (): void {
grid.add({ test: 'me', number: 4, boo: true })
expect(grid.toString()).toEqual(
'[{foo: foo}, {test: me, number: 4.0, boo: true}]'
'[{foo: foo}, {test: me, number: 4, boo: true}]'
)
})
}) // #toString()
Expand Down
2 changes: 1 addition & 1 deletion spec/core/HList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ describe('HList', function (): void {
describe('#toString()', function (): void {
it('returns a human readable string', function (): void {
list.push(null)
expect(list.toString()).toBe('[foovalue, 99.0, ✔, null]')
expect(list.toString()).toBe('[foovalue, 99, ✔, null]')
})
}) // #toString()

Expand Down
90 changes: 90 additions & 0 deletions spec/core/HNum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
POSITIVE_INFINITY_ZINC,
NEGATIVE_INFINITY_ZINC,
NOT_A_NUMBER_ZINC,
DEFAULT_PRECISION,
} from '../../src/core/HNum'
import { Kind } from '../../src/core/Kind'
import { HGrid } from '../../src/core/HGrid'
Expand Down Expand Up @@ -140,6 +141,14 @@ describe('HNum', function (): void {
}) //#unit()

describe('#toString()', function (): void {
it('returns the integer encoded as a string', function (): void {
expect(HNum.make(34).toString()).toBe('34')
})

it('returns the integer encoded as a string with precision', function (): void {
expect(HNum.make(34).toString(4)).toBe('34')
})

it('returns the number encoded as a string', function (): void {
expect(HNum.make(34.6).toString()).toBe('34.6')
})
Expand Down Expand Up @@ -175,6 +184,87 @@ describe('HNum', function (): void {
it('returns NaN for not a number', function (): void {
expect(HNum.make(Number.NaN).toString()).toBe('NaN')
})

it('returns the number with U.S. locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'en-us',
})
).toBe('1,000.5')
})

it('returns the number with Great Britain locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'en-gb',
})
).toBe('1,000.5')
})

it('returns the number with Italys locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'it-it',
})
).toBe('1.000,5')
})

it('returns the number with Frances locale formatting', () => {
// Handle escaped space.
const formattedNumber = HNum.make(1000.5)
.toString({ precision: DEFAULT_PRECISION, locale: 'fr-fr' })
.replace(/\s/g, ' ')

expect(formattedNumber).toBe('1 000,5')
})

it('returns the number with Germanys locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'de-de',
})
).toBe('1.000,5')
})

it('returns the number with Spains locale formatting', () => {
expect(
HNum.make(10000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'es-es',
})
).toBe('10.000,5')
})

it('returns the number with Netherlands locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'nl-nl',
})
).toBe('1.000,5')
})

it('returns the number with Netherlands (Belgium) locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'nl-be',
})
).toBe('1.000,5')
})

it('returns the number with Chinas locale formatting', () => {
expect(
HNum.make(1000.5).toString({
precision: DEFAULT_PRECISION,
locale: 'zh-cn',
})
).toBe('1,000.5')
})
}) // #toString()

describe('#equals()', function (): void {
Expand Down
2 changes: 1 addition & 1 deletion spec/filter/FilterLexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ describe('FilterLexer', function (): void {

const value = lexer.nextToken()
expect(value.type).toBe(TokenType.number)
expect(value.toString()).toBe('1.0')
expect(value.toString()).toBe('1')

expect(TokenType[lexer.nextToken().type]).toBe(
TokenType[TokenType.rightBrace]
Expand Down
34 changes: 30 additions & 4 deletions src/core/HNum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ export const POSITIVE_INFINITY_ZINC = 'INF'
export const NEGATIVE_INFINITY_ZINC = '-INF'
export const NOT_A_NUMBER_ZINC = 'NaN'

/**
* Number formatting options.
*/
export interface NumberFormatOptions {
/**
* The precision to use.
*/
precision: number

/**
* The locale to use (i.e en-US).
*/
locale?: string
}

/**
* Haystack number with units.
*/
Expand Down Expand Up @@ -205,21 +220,32 @@ export class HNum implements HVal {
/**
* Return the number as a readable string.
*
* @param precision Optional precision. Default is 1 decimal place.
* @param params The number format options. Can also be a number for precision
* to support backwards compatibility.
* @returns A string representation of the value.
*/
public toString(precision: number = DEFAULT_PRECISION): string {
public toString(options?: NumberFormatOptions | number): string {
let precision = DEFAULT_PRECISION
let locale: string | undefined

if (typeof options === 'number') {
precision = options
} else if (options) {
precision = options.precision
locale = options.locale
}

if (this.value === Number.POSITIVE_INFINITY) {
return POSITIVE_INFINITY_ZINC
} else if (this.value === Number.NEGATIVE_INFINITY) {
return NEGATIVE_INFINITY_ZINC
} else if (isNaN(this.value)) {
return NOT_A_NUMBER_ZINC
} else {
const value = this.value.toLocaleString(/*locale*/ undefined, {
const value = this.value.toLocaleString(locale, {
style: 'decimal',
maximumFractionDigits: precision,
minimumFractionDigits: precision,
minimumFractionDigits: 0,
})

return this.#unitSymbol ? value + this.#unitSymbol : value
Expand Down

0 comments on commit be5e721

Please sign in to comment.