Skip to content

Commit

Permalink
strict typescipt. 16.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmaad committed Jan 11, 2024
1 parent 988f8db commit b3c23c2
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 66 deletions.
1 change: 1 addition & 0 deletions docs/3rdpartylicenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ THE SOFTWARE.


ng-units
MIT
MIT License

Copyright (c) 2018 Hannes Kamecke
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
<style>:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";--font-family-monospace:SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace}*,*:before,*:after{box-sizing:border-box}html{font-family:sans-serif;line-height:1.15;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:rgba(0,0,0,0)}body{margin:0;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,Liberation Sans,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;text-align:left;background-color:#fff}@media print{*,*:before,*:after{text-shadow:none!important;box-shadow:none!important}@page{size:a3}body{min-width:992px!important}}html{position:relative;min-height:100%}body,html{margin:0}body{margin-bottom:12rem}</style><link rel="stylesheet" href="styles.c304b9ff1da0c16f.css" media="print" onload="this.media='all'"><noscript><link rel="stylesheet" href="styles.c304b9ff1da0c16f.css"></noscript></head>
<body>
<app-root></app-root>
<script src="runtime.7d11e06b23d743f5.js" type="module"></script><script src="polyfills.2101d3d83ea8e419.js" type="module"></script><script src="main.14abfd2b79b0daa1.js" type="module"></script></body>
<script src="runtime.7d11e06b23d743f5.js" type="module"></script><script src="polyfills.2101d3d83ea8e419.js" type="module"></script><script src="main.4c9965b66876bb38.js" type="module"></script></body>
</html>

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions projects/ng-units-doc/src/app/demo/main/main.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import { Component, OnInit } from '@angular/core';
selector: 'ng-main',
templateUrl: './main.component.html'
})
export class MainComponent implements OnInit {
export class MainComponent {

value: number;

constructor() { }

ngOnInit() {
this.value = 265;
}
value = 265;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { systemOfUnitsInitializer } from '../../system-of-units-initializer';
})
export class BasicsComponent implements OnInit {

length: Quantity;
length?: Quantity;
quantity = new Quantity(length);
value = 1.25;

Expand Down
2 changes: 1 addition & 1 deletion projects/ng-units/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-units",
"version": "16.0.1",
"version": "16.0.2",
"license": "MIT",
"description": "Angular component library for units of measurement.",
"keywords": ["Angular", "Library"],
Expand Down
2 changes: 1 addition & 1 deletion projects/ng-units/src/lib/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type QuantityFormatter = (value: number) => string;
export const QuantityFormatters: { [name: string]: QuantityFormatter } = {
'default': function (value: number): string {
const abs = Math.abs(value);
let text;
let text: string;
if (abs && (abs >= 1e5 || abs <= 1e-2)) {
text = removeZeroDigits(value.toExponential(3));
}
Expand Down
4 changes: 2 additions & 2 deletions projects/ng-units/src/lib/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type QuantityParser = (value: string|number) => number;
export type QuantityParser = (value?: string|number|null) => number|null|undefined;

export const QuantityParsers: { [name: string]: QuantityParser } = {

'default' : function(value: string|number) {
'default': function(value?: string|number|null) {
if (typeof value === 'string') {
const str = value.replace(',', '.');
return str === '' ? null : Number(str);
Expand Down
6 changes: 3 additions & 3 deletions projects/ng-units/src/lib/quantity.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Unit } from './unit';
})
class QuantityDirectiveTestComponent {
value = 1;
quantity: Quantity;
quantity?: Quantity;
unit: string|Unit = '';
}

Expand All @@ -41,7 +41,7 @@ describe('QuantityDirective', () => {
units = TestBed.inject(SystemOfUnits);
units.add(new Quantity(length));
units.add(new Quantity(area));
units.get('Length').selectUnit('cm');
units.get('Length')?.selectUnit('cm');

fixture = TestBed.createComponent(QuantityDirectiveTestComponent);
fixture.detectChanges();
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('QuantityDirective', () => {
});

it('should convert back using unit instance binding', async () => {
fixture.componentInstance.unit = units.get('Length').findUnit('mm');
fixture.componentInstance.unit = units.get('Length')?.findUnit('mm') || '';
fixture.detectChanges();
await fixture.whenStable();
await set('#with-unit-binding', 1200);
Expand Down
21 changes: 11 additions & 10 deletions projects/ng-units/src/lib/quantity.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ const CONTROL_VALUE_ACCESSOR = {
})
export class QuantityDirective implements ControlValueAccessor, OnInit, OnChanges, OnDestroy {

@Input('ngQuantity') quantityAttr: string | Quantity;
@Input('ngQuantity') quantityAttr?: string | Quantity;
@Input() ngUnit?: string|Unit;

quantity: Quantity;
quantity?: Quantity;

private inputElement: HTMLInputElement;
private onTouch: () => void;
private onModelChange: (value: string|number) => void;
private currentModelValue: string|number;
private subscription: Subscription;
private inputElement!: HTMLInputElement;
private onTouch?: () => void;
private onModelChange?: (value: string|number|undefined|null) => void;
private currentModelValue?: string|number|null;
private subscription?: Subscription;

constructor(private elementRef: ElementRef, private system: SystemOfUnits) {

Expand Down Expand Up @@ -98,7 +98,7 @@ export class QuantityDirective implements ControlValueAccessor, OnInit, OnChange
this.onTouch = fn;
}

registerOnChange(fn: (value: string|number) => void) {
registerOnChange(fn: (value: string|number|undefined|null) => void) {
this.onModelChange = fn;
}

Expand Down Expand Up @@ -126,7 +126,7 @@ export class QuantityDirective implements ControlValueAccessor, OnInit, OnChange
this.updateView(rawValue);
}

private updateView(modelValue: string|number) {
private updateView(modelValue: string|number|undefined|null) {
if (!this.quantity) {
this.inputElement.value = defaultPrint(modelValue);
return;
Expand All @@ -136,7 +136,8 @@ export class QuantityDirective implements ControlValueAccessor, OnInit, OnChange
this.inputElement.value = this.quantity.print(converted, false);
}
else {
this.inputElement.value = null;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.inputElement.value = null!;
}
}

Expand Down
6 changes: 3 additions & 3 deletions projects/ng-units/src/lib/quantity.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SystemOfUnits } from './system-of-units.service';
})
class QuantityPipeTestComponent {
value = 1;
quantity;
quantity?: Quantity|string;
}

describe('QuantityPipe', () => {
Expand Down Expand Up @@ -126,8 +126,8 @@ describe('QuantityPipe', () => {

let systemQuantity: Quantity;
let fixture: ComponentFixture<QuantityPipeTestComponent>;
let byInstance;
let byName;
let byInstance: Element;
let byName: Element;
beforeEach(() => {
systemQuantity = new Quantity(length);
systemOfUnits.add(systemQuantity);
Expand Down
6 changes: 3 additions & 3 deletions projects/ng-units/src/lib/quantity.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export class QuantityPipe implements PipeTransform {
constructor(private system: SystemOfUnits) {
}

transform(value: string | number,
transform(value: string | number | null | undefined,
quantity?: Quantity | string,
addSymbolOrUnit?: boolean|Unit|string,
addSymbol?: boolean): string
addSymbol?: boolean): string|undefined
{
if (typeof quantity === 'string') {
quantity = this.system.get(quantity);
}

let unit: string|Unit;
let unit: string|Unit|undefined;
if (typeof addSymbolOrUnit === 'boolean') {
addSymbol = addSymbolOrUnit;
}
Expand Down
67 changes: 63 additions & 4 deletions projects/ng-units/src/lib/quantity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Quantity, QuantityDefinition } from './quantity';
import { Quantity, QuantityDefinition } from './quantity';
import { SimpleUnit } from './unit';


Expand All @@ -10,9 +10,9 @@ describe('Quantity', () => {
definition = {
name: 'Length',
units: {
'm' : [1],
'cm' : [100],
'ΩΩ' : [10, 10]
'm': [1],
'cm': [100],
'ΩΩ': [10, 10]
}
};
});
Expand Down Expand Up @@ -175,6 +175,11 @@ describe('Quantity', () => {
expect(quantity.toBase('2.0e-02')).toBe(2e-4);
expect(quantity.toBase('2,0e-02')).toBe(2e-4);
});

it('should use a custom parser', () => {
quantity.parser = value => +(value || 0) * 2;
expect(quantity.toBase('2')).toBe(0.04);
});
});

describe('fromBase', function () {
Expand Down Expand Up @@ -223,5 +228,59 @@ describe('Quantity', () => {
expect(quantity.fromBase('2.0e-02')).toBe(2);
expect(quantity.fromBase('2,0e-02')).toBe(2);
});

it('should use a custom parser', () => {
quantity.parser = value => {
if (value === '2') {
return 222;
}
return null;
};
const value = quantity.fromBase('2');
expect(value).toBe(22200);
});
});

describe('findUnit', () => {
let quantity: Quantity;

beforeEach(() => {
// Create a new instance of Quantity for each test case
quantity = new Quantity();
quantity.units = [
new SimpleUnit('m', 1, 0),
new SimpleUnit('cm', 0.01, 0),
new SimpleUnit('ft', 0.3048, 0),
];
});

it('should return undefined if no unit is provided', () => {
const result = quantity.findUnit(undefined);
expect(result).toBeUndefined();
});

it('should return the unit if it exists in the units array', () => {
const unit = new SimpleUnit('m', 1, 0);
const result = quantity.findUnit(unit);
expect(result).toEqual(unit);
});

it('should return undefined if the unit does not exist in the units array', () => {
const unit = new SimpleUnit('km', 1000, 0);
const result = quantity.findUnit(unit);
expect(result).toBeUndefined();
});

it('should return the unit with the matching symbol', () => {
const symbol = 'ft';
const result = quantity.findUnit(symbol);
expect(result?.symbol).toEqual(symbol);
});

it('should return undefined if the unit symbol is an empty string', () => {
const symbol = '';
const result = quantity.findUnit(symbol);
expect(result).toBeUndefined();
});
});
});
28 changes: 14 additions & 14 deletions projects/ng-units/src/lib/quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export interface QuantityDefinition {


export class Quantity {
name: string;
unit: Unit;
name!: string;
unit!: Unit;
units: Unit[] = [];
formatter: QuantityFormatter;
parser: QuantityParser;
formatter!: QuantityFormatter;
parser!: QuantityParser;

constructor(definition?: QuantityDefinition) {
if (definition) {
Expand All @@ -33,28 +33,28 @@ export class Quantity {
}
}

fromBase(value: string|number, unit?: Unit|string): number {
fromBase(value: string|number|undefined|null, unit?: Unit|string): number|null {
const n = this.parser(value);
return isNotNumeric(n) ? null : this.getUnit(unit).fromBase(n);
return isNumeric(n) ? this.getUnit(unit).fromBase(n) : null;
}

toBase(value: string|number, unit?: Unit|string): number {
toBase(value: string|number|null|undefined, unit?: Unit|string): number|null {
const n = this.parser(value);
return isNotNumeric(n) ? null : this.getUnit(unit).toBase(n);
return isNumeric(n) ? this.getUnit(unit).toBase(n) : null;
}

private getUnit(unit?: string|Unit) {
return this.findUnit(unit) || this.unit;
}

selectUnit(unit: string|Unit) {
selectUnit(unit?: string|Unit|null) {
const u = this.findUnit(unit);
if (u) {
this.unit = u;
}
}

findUnit(unit: string|Unit): Unit|undefined {
findUnit(unit?: string|Unit|null): Unit|undefined {
if (!unit) {
return;
}
Expand All @@ -71,7 +71,7 @@ export class Quantity {
* Returns a string containing formatted number and unit symbol (optional).
* @param value string or number
*/
print(value: string|number, addUnitSymbol?: boolean, unit?: string|Unit) {
print(value: string|number|null|undefined, addUnitSymbol?: boolean, unit?: string|Unit) {
const number = this.parser(value);
if (typeof number !== 'number') {
return '';
Expand All @@ -81,11 +81,11 @@ export class Quantity {
}
}

function isNotNumeric(value) {
return value === null || isNaN(value);
function isNumeric(value: unknown): value is number {
return typeof value === 'number' && !isNaN(value);
}

export function defaultPrint(value: string|number): string {
export function defaultPrint(value: string|number|undefined|null): string {
const num = QuantityParsers['default'](value);
return typeof num === 'number' ? QuantityFormatters['default'](num) : '';
}
4 changes: 3 additions & 1 deletion projects/ng-units/src/lib/system-of-units.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('QuantityService', () => {
sub.unsubscribe();
done();
});
systemOfUnits.selectUnit(l, 'mm');
if (l) {
systemOfUnits.selectUnit(l, 'mm');
}
});

});
6 changes: 3 additions & 3 deletions projects/ng-units/src/lib/system-of-units.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { filter } from 'rxjs/operators';


export class QuantityMessage {
quantity: Quantity;
quantity?: Quantity;
}

@Injectable()
Expand All @@ -22,7 +22,7 @@ export class SystemOfUnits {
this.quantities.push(...quantities);
}

get(quantityName: string): Quantity {
get(quantityName: string): Quantity|undefined {
const quantity = this.quantities.find(q => q.name === quantityName);
return quantity;
}
Expand All @@ -42,7 +42,7 @@ export class SystemOfUnits {
/**
* @depracted since 11.0.0. Use changes$ instead.
*/
subscribe(quantity, callback: (m: QuantityMessage) => unknown): Subscription {
subscribe(quantity: Quantity, callback: (m: QuantityMessage) => unknown): Subscription {
return this.quantityChange.pipe(
filter(m => m.quantity === quantity))
.subscribe(callback);
Expand Down
Loading

0 comments on commit b3c23c2

Please sign in to comment.