Skip to content

Commit

Permalink
Negative values (#12)
Browse files Browse the repository at this point in the history
* updated pika

* fixes #10
  • Loading branch information
varna committed Aug 28, 2019
1 parent 17d3244 commit 0db1f9c
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 124 deletions.
33 changes: 1 addition & 32 deletions __tests__/index.ts → __tests__/Measure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Measure, { measureFrom } from '../src'
import Measure from '../src'

describe('Measure Class', () => {
test('.valueOf()', () => {
Expand Down Expand Up @@ -32,37 +32,6 @@ describe('Floats', () => {
expect(Number(new Measure(1 + Number.EPSILON))).toBe(1))
})

describe('Rounding e2', () => {
const selector = measureFrom('m', 'cm')

test('1.11', () => expect(String(selector(1.11))).toBe('1.11 m'))
test('1.111', () => expect(String(selector(1.111))).toBe('1.11 m'))
test('5.55', () => expect(String(selector(5.55))).toBe('5.55 m'))
test('5.555', () => expect(String(selector(5.555))).toBe('5.56 m'))
test('9.99', () => expect(String(selector(9.99))).toBe('9.99 m'))
test('9.999', () => expect(String(selector(9.999))).toBe('10 m'))
test('0.001', () => expect(String(selector(0.001))).toBe('0.1 cm'))
test('0.0001', () => expect(String(selector(0.0001))).toBe('0.01 cm'))
test('0.00001', () => expect(String(selector(0.00001))).toBe('0 cm'))
test('0.00005', () => expect(String(selector(0.00005))).toBe('0.01 cm'))
})

// describe('Rounding e3', () => {
// Measure.precision = 3
// const selector = measureFrom('m', 'cm')

// test('1.111', () => expect(String(selector(1.111))).toBe('1.111 m'))
// test('1.1111', () => expect(String(selector(1.1111))).toBe('1.111 m'))
// test('5.555', () => expect(String(selector(5.555))).toBe('5.555 m'))
// test('5.5555', () => expect(String(selector(5.5555))).toBe('5.556 m'))
// test('9.999', () => expect(String(selector(9.999))).toBe('9.999 m'))
// test('9.9999', () => expect(String(selector(9.9999))).toBe('10 m'))
// test('0.0001', () => expect(String(selector(0.0001))).toBe('0.01 cm'))
// test('0.00001', () => expect(String(selector(0.00001))).toBe('0.001 cm'))
// test('0.000001', () => expect(String(selector(0.000001))).toBe('0 cm'))
// test('0.000005', () => expect(String(selector(0.000005))).toBe('0.001 cm'))
// })

test('Convert and restore', () => {
const meter = new Measure(1)
const res = String(meter.to('dm').to('m'))
Expand Down
32 changes: 32 additions & 0 deletions __tests__/measureFrom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { measureFrom } from '../src'

describe('Rounding e2', () => {
const selector = measureFrom('m', 'cm')

test('1.11', () => expect(String(selector(1.11))).toBe('1.11 m'))
test('1.111', () => expect(String(selector(1.111))).toBe('1.11 m'))
test('5.55', () => expect(String(selector(5.55))).toBe('5.55 m'))
test('5.555', () => expect(String(selector(5.555))).toBe('5.56 m'))
test('9.99', () => expect(String(selector(9.99))).toBe('9.99 m'))
test('9.999', () => expect(String(selector(9.999))).toBe('10 m'))
test('0.001', () => expect(String(selector(0.001))).toBe('0.1 cm'))
test('0.0001', () => expect(String(selector(0.0001))).toBe('0.01 cm'))
test('0.00001', () => expect(String(selector(0.00001))).toBe('0 cm'))
test('0.00005', () => expect(String(selector(0.00005))).toBe('0.01 cm'))
})

describe('Rounding negative e2', () => {
const selector = measureFrom('m', 'cm')

test('-1.11', () => expect(String(selector(-1.11))).toBe('-1.11 m'))
test('-1.111', () => expect(String(selector(-1.111))).toBe('-1.11 m'))
test('-5.55', () => expect(String(selector(-5.55))).toBe('-5.55 m'))
test('-5.555', () => expect(String(selector(-5.555))).toBe('-5.56 m'))
test('-9.99', () => expect(String(selector(-9.99))).toBe('-9.99 m'))
test('-9.999', () => expect(String(selector(-9.999))).toBe('-10 m'))
test('-0.001', () => expect(String(selector(-0.001))).toBe('-0.1 cm'))
test('-0.0001', () => expect(String(selector(-0.0001))).toBe('-0.01 cm'))
test('-0.00001', () => expect(String(selector(-0.00001))).toBe('0 cm'))
test('-0.00005', () => expect(String(selector(-0.00005))).toBe('-0.01 cm'))
test('-0.00009', () => expect(String(selector(-0.00009))).toBe('-0.01 cm'))
})
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"print"
],
"scripts": {
"build": "pika build",
"version": "pack build",
"test": "jest",
"dev": "jest --watch"
Expand All @@ -40,14 +41,14 @@
]
},
"devDependencies": {
"@pika/pack": "^0.4.0",
"@pika/plugin-build-node": "^0.4.0",
"@pika/plugin-build-web": "^0.4.0",
"@pika/plugin-ts-standard-pkg": "^0.4.0",
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.5.0",
"@pika/plugin-build-web": "^0.5.0",
"@pika/plugin-ts-standard-pkg": "^0.5.0",
"@types/jest": "^24.0.15",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"typescript": "^3.5.2"
},
"dependencies": {}
}
}
6 changes: 5 additions & 1 deletion src/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export default class Measure extends Number implements Number {
*/
public toString = (): string => {
const pow = Math.pow(10, this.round)
const rounded = Math.round(this.valueOf() * pow + Number.EPSILON) / pow
const value = this.valueOf()
const sign = value < 0 ? -1 : 1
const absoluteValue = value * sign
const rounded =
(Math.round(absoluteValue * pow + Number.EPSILON) / pow) * sign
return `${rounded} ${this.prefix}${this.symbol}${this.suffix}`
}
}
14 changes: 9 additions & 5 deletions src/measureSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ const createMeasureSelector = (...unitKeys: UnitKey[]) => {
* @returns {Measure} new Measure(value, bestLookingUnit)
*/
const finder = (value: number) => {
const sign = value < 0 ? -1 : 1
const absoluteValue = value * sign
// console.log({ value, absoluteValue, sign })

const found = units.reduce(
// find smallest unit larger than 1
(previousUnit, currentUnit) => {
const currentQuantity = Number(toMeasure(currentUnit, value))
const previousQuantity = Number(toMeasure(previousUnit, value))
const currentQuantity = Number(toMeasure(currentUnit, absoluteValue))
const previousQuantity = Number(toMeasure(previousUnit, absoluteValue))

return previousQuantity < 1 || currentQuantity < previousQuantity
? currentUnit
Expand All @@ -46,11 +50,11 @@ const createMeasureSelector = (...unitKeys: UnitKey[]) => {
units[0] // default to first/largest unit
)

const foundMeasure = toMeasure(found, value)
const foundMeasure = toMeasure(found, absoluteValue)
if (Number(foundMeasure) < 1) {
return toMeasure(units[units.length - 1], value)
return toMeasure(units[units.length - 1], absoluteValue * sign)
} else {
return foundMeasure
return toMeasure(found, absoluteValue * sign)
}
}

Expand Down
Loading

0 comments on commit 0db1f9c

Please sign in to comment.