Skip to content

Commit

Permalink
Merge branch 'elliotmendiola-backspace-separator'
Browse files Browse the repository at this point in the history
  • Loading branch information
nbfontana committed Sep 10, 2018
2 parents 2583f74 + 4b490e7 commit 32817ac
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"mocha": "3.3.0",
"nyc": "^11.0.3",
"rxjs": "5.0.1",
"sinon": "2.0.0",
"sinon": "^6.2.0",
"sinon-chai": "2.8.0",
"standard-version": "4.0.0",
"ts-loader": "2.1.0",
Expand Down
27 changes: 20 additions & 7 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class InputService {
this.PER_AR_NUMBER.set("\u0669", "9");
}

private inputManager: InputManager;
inputManager: InputManager;

constructor(private htmlInputElement: any, private options: CurrencyMaskConfig) {
this.inputManager = new InputManager(htmlInputElement);
Expand Down Expand Up @@ -81,7 +81,6 @@ export class InputService {
}

clearMask(rawValue: string): number {

if (this.isNullable() && rawValue === "")
return null;

Expand Down Expand Up @@ -126,11 +125,25 @@ export class InputService {
selectionStart = this.rawValue.length - this.options.suffix.length;
}

selectionEnd = keyCode == 46 || keyCode == 63272 ? selectionEnd + 1 : selectionEnd;
selectionStart = keyCode == 8 ? selectionStart - 1 : selectionStart;
this.rawValue = this.rawValue.substring(0, selectionStart) + this.rawValue.substring(selectionEnd, this.rawValue.length);
this.updateFieldValue(selectionStart);
}
let move = this.rawValue.substr(selectionStart - 1, 1).match(/\d/) ? 0 : -1;
if ((
keyCode == 8 &&
selectionStart - 1 === 0 &&
!(this.rawValue.substr(selectionStart, 1).match(/\d/))
) ||
(
(keyCode == 46 || keyCode == 63272) &&
selectionStart === 0 &&
!(this.rawValue.substr(selectionStart + 1, 1).match(/\d/))
)
) {
move = 1;
};
selectionEnd = keyCode == 46 || keyCode == 63272 ? selectionEnd + 1 : selectionEnd;
selectionStart = keyCode == 8 ? selectionStart - 1 : selectionStart;
this.rawValue = this.rawValue.substring(0, selectionStart) + this.rawValue.substring(selectionEnd, this.rawValue.length);
this.updateFieldValue(selectionStart + move);
}

updateFieldValue(selectionStart?: number): void {
let newRawValue = this.applyMask(false, this.rawValue || "");
Expand Down
58 changes: 58 additions & 0 deletions test/input-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { InputService } from './../src/input.service';
import { fakeAsync } from "@angular/core/testing";
import { expect } from "chai";
import { stub } from 'sinon';
import {CurrencyMaskConfig} from "../src/currency-mask.config";

describe('Testing InputService', () => {
var options: CurrencyMaskConfig = {
prefix: '',
suffix: '',
thousands: '.',
decimal: ',',
allowNegative: false,
nullable: false,
align: 'right',
allowZero: true,
precision: undefined,
};
var inputService: InputService;

describe('removeNumber', () => {
it('should call updateFieldValue with 1 when deleting the first number followed by a .', () => {
inputService = new InputService({
selectionStart: 0,
selectionEnd: 0,
}, options);

inputService.inputManager.rawValue = '1.234,50';
inputService.updateFieldValue = stub();
inputService.removeNumber(46);
expect(inputService.updateFieldValue).to.be.calledWith(1);
});

it('should call updateFieldValue with 1 when backspacing the first number followed by a .', () => {
inputService = new InputService({
selectionStart: 1,
selectionEnd: 1,
}, options);

inputService.inputManager.rawValue = '1.234,50';
inputService.updateFieldValue = stub();
inputService.removeNumber(8);
expect(inputService.updateFieldValue).to.be.calledWith(1);
});

it('should call updateFieldValue with 2 less than the current position when backspacing any non-number character', () => {
inputService = new InputService({
selectionStart: 6,
selectionEnd: 6,
}, options);

inputService.inputManager.rawValue = '1.234,50';
inputService.updateFieldValue = stub();
inputService.removeNumber(8);
expect(inputService.updateFieldValue).to.be.calledWith(4);
});
});
});

0 comments on commit 32817ac

Please sign in to comment.