diff --git a/package-lock.json b/package-lock.json index a21f2bbd..8288b7e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "lnp2pbot", - "version": "0.14.2", + "version": "0.14.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "lnp2pbot", - "version": "0.14.2", + "version": "0.14.3", "license": "MIT", "dependencies": { "@grammyjs/i18n": "^0.5.1", @@ -2470,6 +2470,7 @@ "version": "0.5.0", "resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz", "integrity": "sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==", + "license": "MIT", "dependencies": { "tslib": "^2.4.0" } diff --git a/tests/util/index.spec.ts b/tests/util/index.spec.ts new file mode 100644 index 00000000..7df03a23 --- /dev/null +++ b/tests/util/index.spec.ts @@ -0,0 +1,139 @@ +/* eslint-disable no-unused-expressions */ +import { + getCurrency, + numberFormat, + decimalRound, + plural, + isFloat, + toKebabCase, +} from '../../util/index'; + +const { expect } = require('chai'); + +describe('Utility Functions', () => { + describe('getCurrency', () => { + it('should return currency object for valid ISO code', () => { + const result = getCurrency('USD'); + expect(result).to.not.be.null; + if (result) { + expect(result).to.have.property('symbol'); + expect(result).to.have.property('locale'); + } + }); + + it('should return null for invalid currency code', () => { + const result = getCurrency('INVALID'); + expect(result).to.be.null; + }); + + it('should return null for empty string', () => { + const result = getCurrency(''); + expect(result).to.be.null; + }); + + it('should handle common currency codes', () => { + const usd = getCurrency('USD'); + const eur = getCurrency('EUR'); + + expect(usd).to.not.be.null; + expect(eur).to.not.be.null; + }); + }); + + describe('numberFormat', () => { + it('should return false for invalid currency code', () => { + const result = numberFormat('INVALID', 100); + expect(result).to.equal(false); + }); + + it('should return original number if no locale found', () => { + const result = numberFormat('USD', 1234.56); + expect(result).to.not.be.false; + }); + + it('should return original number for NaN input', () => { + const result = numberFormat('USD', NaN); + expect(result).to.be.NaN; + }); + + it('should handle zero correctly', () => { + const result = numberFormat('USD', 0); + expect(result).to.not.be.false; + }); + + it('should handle negative numbers', () => { + const result = numberFormat('USD', -100); + expect(result).to.not.be.false; + }); + }); + + describe('decimalRound', () => { + it('should round to integer when exp is 0', () => { + expect(decimalRound(3.7, 0)).to.equal(4); + expect(decimalRound(3.2, 0)).to.equal(3); + }); + + it('should round to specified decimal places', () => { + expect(decimalRound(3.14159, -2)).to.equal(3.14); + expect(decimalRound(3.16159, -2)).to.equal(3.16); + expect(decimalRound(10.995, -2)).to.equal(11); + }); + + it('should handle single decimal place', () => { + expect(decimalRound(3.16, -1)).to.equal(3.2); + expect(decimalRound(3.14, -1)).to.equal(3.1); + }); + + it('should return NaN for invalid inputs', () => { + expect(decimalRound('invalid' as any, -2)).to.be.NaN; + expect(decimalRound(3.14, 2.5)).to.be.NaN; + }); + + it('should handle zero correctly', () => { + expect(decimalRound(0, -2)).to.equal(0); + expect(decimalRound(0.001, -2)).to.equal(0); + }); + + it('should handle negative numbers', () => { + expect(decimalRound(-3.14159, -2)).to.equal(-3.14); + expect(decimalRound(-3.16159, -2)).to.equal(-3.16); + }); + }); + + describe('plural', () => { + it('should return empty string for 1', () => { + const result = plural(1); + expect(result).to.equal(''); + }); + + it('should return "s" for numbers other than 1', () => { + expect(plural(0)).to.equal('s'); + expect(plural(2)).to.equal('s'); + expect(plural(5)).to.equal('s'); + }); + }); + + describe('isFloat', () => { + it('should return true for float numbers', () => { + expect(isFloat(3.14)).to.be.true; + expect(isFloat(0.5)).to.be.true; + }); + + it('should return false for integers', () => { + expect(isFloat(5)).to.be.false; + expect(isFloat(0)).to.be.false; + }); + }); + + describe('toKebabCase', () => { + it('should convert camelCase to kebab-case', () => { + const result = toKebabCase('camelCaseString'); + expect(result).to.equal('camel-case-string'); + }); + + it('should convert spaces and underscores to dashes', () => { + expect(toKebabCase('hello world')).to.equal('hello-world'); + expect(toKebabCase('hello_world')).to.equal('hello-world'); + }); + }); +});