• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eclipsesource / jsonforms / 3813777547

pending completion
3813777547

push

github

GitHub
Bump json5 and rollup-plugin-vue in /packages/vue2/vue2-vanilla

109 of 177 branches covered (61.58%)

296 of 355 relevant lines covered (83.38%)

13.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.88
/packages/angular-material/src/controls/number.renderer.ts
1
/*
2
  The MIT License
3
  
4
  Copyright (c) 2017-2019 EclipseSource Munich
5
  https://github.com/eclipsesource/jsonforms
6
  
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
8
  of this software and associated documentation files (the "Software"), to deal
9
  in the Software without restriction, including without limitation the rights
10
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
  copies of the Software, and to permit persons to whom the Software is
12
  furnished to do so, subject to the following conditions:
13
  
14
  The above copyright notice and this permission notice shall be included in
15
  all copies or substantial portions of the Software.
16
  
17
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
  THE SOFTWARE.
24
*/
25
import { ChangeDetectionStrategy, Component } from '@angular/core';
26
import { JsonFormsAngularService, JsonFormsControl } from '@jsonforms/angular';
27
import {
28
  isIntegerControl,
29
  isNumberControl,
30
  or,
31
  RankedTester,
32
  rankWith,
33
  StatePropsOfControl
34
} from '@jsonforms/core';
35
import merge from 'lodash/merge';
36

37
@Component({
38
  selector: 'NumberControlRenderer',
39
  template: `
40
    <mat-form-field fxFlex [fxHide]="hidden">
41
      <mat-label>{{ label }}</mat-label>
42
      <input
43
        matInput
44
        (input)="onChange($event)"
45
        [value]="getValue()"
46
        [id]="id"
47
        [formControl]="form"
48
        [min]="min"
49
        [max]="max"
50
        [step]="multipleOf"
51
        (focus)="focused = true" 
52
        (focusout)="focused = false"
53
      />
54
      <mat-hint *ngIf="shouldShowUnfocusedDescription()" || focused>{{ description }}</mat-hint>
55
      <mat-error>{{ error }}</mat-error>
56
    </mat-form-field>
57
  `,
58
  changeDetection: ChangeDetectionStrategy.OnPush
59
})
60
export class NumberControlRenderer extends JsonFormsControl {
1✔
61
  private readonly MAXIMUM_FRACTIONAL_DIGITS = 20;
15✔
62

63
  oldValue: string;
64
  min: number;
65
  max: number;
66
  multipleOf: number;
67
  locale: string;
68
  numberFormat: Intl.NumberFormat;
69
  decimalSeparator: string;
70

71
  constructor(jsonformsService: JsonFormsAngularService) {
72
    super(jsonformsService);
15!
73
  }
74

75
  onChange(ev: any) {
1✔
76
    const data = this.oldValue
×
77
      ? ev.target.value.replace(this.oldValue, '')
×
78
      : ev.target.value;
79
    // ignore these
80
    if (
×
81
      data === '.' ||
×
82
      data === ',' ||
83
      data === ' ' ||
84
      // if the value is 0 and we already have a value then we ignore
85
      (data === '0' &&
86
        this.getValue() !== '' &&
87
        // a 0 in the first place
88
        ((ev.target.selectionStart === 1 && ev.target.selectionEnd === 1) ||
89
          // or in the last place as this doesn't change the value (when there is a separator)
90
          (ev.target.selectionStart === ev.target.value.length &&
91
            ev.target.selectionEnd === ev.target.value.length &&
92
            ev.target.value.indexOf(this.decimalSeparator) !== -1)))
93
    ) {
94
      this.oldValue = ev.target.value;
×
95
      return;
×
96
    }
97
    super.onChange(ev);
×
98
    this.oldValue = this.getValue();
×
99
  }
100

101
  getEventValue = (event: any) => {
15✔
102
    const cleanPattern = new RegExp(`[^-+0-9${this.decimalSeparator}]`, 'g');
×
103
    const cleaned = event.target.value.replace(cleanPattern, '');
×
104
    const normalized = cleaned.replace(this.decimalSeparator, '.');
×
105

106
    if (normalized === '') {
×
107
      return undefined;
×
108
    }
109

110
    // convert to number
111
    const number = +normalized;
×
112
    // if not a number just return the string
113
    if (Number.isNaN(number)) {
×
114
      return event.target.value;
×
115
    }
116
    return number;
×
117
  };
118

119
  getValue = () => {
15✔
120
    if (this.data !== undefined && this.data !== null) {
73✔
121
      if (typeof this.data === 'number') {
67!
122
        return this.numberFormat.format(this.data);
67✔
123
      }
124
      return this.data;
×
125
    }
126
    return '';
6✔
127
  };
128

129
  mapAdditionalProps(props:StatePropsOfControl) {
1✔
130
    if (this.scopedSchema) {
38!
131
      const testerContext = {
38✔
132
        rootSchema: this.rootSchema,
133
        config: props.config
134
      }
135
      const defaultStep = isNumberControl(this.uischema, this.rootSchema, testerContext)
38✔
136
        ? 0.1
38✔
137
        : 1;
138
      this.min = this.scopedSchema.minimum;
38✔
139
      this.max = this.scopedSchema.maximum;
38✔
140
      this.multipleOf = this.scopedSchema.multipleOf || defaultStep;
38✔
141
      const appliedUiSchemaOptions = merge({}, props.config, this.uischema.options);
38✔
142
      const currentLocale = this.jsonFormsService.getLocale();
38✔
143
      if (this.locale === undefined || this.locale !== currentLocale) {
38✔
144
        this.locale = currentLocale;
15✔
145
        this.numberFormat = new Intl.NumberFormat(this.locale, {
15✔
146
          useGrouping: appliedUiSchemaOptions.useGrouping,
147
          maximumFractionDigits: this.MAXIMUM_FRACTIONAL_DIGITS
148
        });
149
        this.determineDecimalSeparator();
15✔
150
        this.oldValue = this.getValue();
15✔
151
      }
152
      this.form.setValue(this.getValue());
38✔
153
    }
154
  }
155

156
  private determineDecimalSeparator(): void {
1✔
157
    const example = this.numberFormat.format(1.1);
15✔
158
    this.decimalSeparator = example.charAt(1);
15✔
159
  }
160
}
1✔
161
export const NumberControlRendererTester: RankedTester = rankWith(
1✔
162
  2,
163
  or(isNumberControl, isIntegerControl)
164
);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc