Skip to content

Commit

Permalink
adds changes to allow dom.Decimal construction from Number and `S…
Browse files Browse the repository at this point in the history
…tring` (#746)

* adds changes for dom.Decimal construction
* updates uglify version
  • Loading branch information
desaikd committed Mar 6, 2023
1 parent 7ae89cf commit bef5f15
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"grunt-contrib-clean": "^2.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-jshint": "^2.1.0",
"grunt-contrib-uglify": "^4.0.1",
"grunt-contrib-uglify": "^5.2.2",
"grunt-eslint": "^23.0.0",
"grunt-shell": "^3.0.1",
"grunt-ts": "^6.0.0-beta.22",
Expand Down
46 changes: 41 additions & 5 deletions src/dom/Decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,49 @@ export class Decimal extends Value(

/**
* Constructor.
* @param value The numeric value to represent as a decimal.
* @param value The Ion decimal value to represent as a decimal.
* @param annotations An optional array of strings to associate with `value`.
*/
constructor(value: ion.Decimal, annotations: string[] = []) {
super(value.numberValue());
this._decimalValue = value;
this._numberValue = value.numberValue();
constructor(value: ion.Decimal, annotations?: string[]);

/**
* Constructor.
* @param value The text Ion value to be parsed as a decimal.
* @param annotations An optional array of strings to associate with `value`.
*/
constructor(value: string, annotations?: string[]);

/**
* Constructor.
* @param value The number value to represent as a decimal.
* @param annotations An optional array of strings to associate with `value`.
*/
constructor(value: number, annotations?: string[]);

// This is the unified implementation of the above signatures and is not visible to users.
constructor(
value: ion.Decimal | string | number,
annotations: string[] = []
) {
if (typeof value === "string") {
let numberValue = Number(value);
super(numberValue);
this._decimalValue = new ion.Decimal(value);
this._numberValue = numberValue;
} else if (value instanceof ion.Decimal) {
super(value.numberValue());
this._decimalValue = value;
this._numberValue = value.numberValue();
} else if (typeof value === "number") {
// if value is a number type
super(value);
this._decimalValue = new ion.Decimal("" + value);
this._numberValue = value;
} else {
throw new Error(
"Decimal value can only be created from number, ion.Decimal or string"
);
}
this._setAnnotations(annotations);
}

Expand Down
5 changes: 4 additions & 1 deletion test/dom/dom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "chai";
import { load, loadAll, Value } from "../../src/dom";
import * as ion from "../../src/Ion";
import { IonTypes } from "../../src/Ion";
import { dom, IonTypes } from "../../src/Ion";
import { encodeUtf8 } from "../../src/IonUnicode";

/**
Expand Down Expand Up @@ -216,6 +216,9 @@ describe("DOM", () => {
assert.equal(101.5, +d);
assert.equal(101.5, d.numberValue());
assert.isTrue(new ion.Decimal("101.5").equals(d.decimalValue()!));
assert.isTrue(new dom.Decimal('101.5').equals(d));
assert.isTrue(new dom.Decimal(101.5).equals(d));
assert.isFalse(new dom.Decimal(101.0).equals(d));
assert.equal(1015, Number(d.decimalValue()!.getCoefficient()));
});

Expand Down

0 comments on commit bef5f15

Please sign in to comment.