Skip to content

Commit 06ce447

Browse files
authored
🔀 Merge pull request #59 from codermarcos/feature/allow-negative
🐛 Fix missing feature allow negative
2 parents 3e6f12a + 156d066 commit 06ce447

8 files changed

+342
-67
lines changed

cypress/e2e/v4.x.x/set-mask.cy.ts

+196-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe(
2222
beforeEach(
2323
() => {
2424
cy.visit(getUrl({ prefix: '$', suffix: 'AUD' }, '6.66'));
25-
cy.reload();
2625
}
2726
);
2827

@@ -59,7 +58,6 @@ describe(
5958
beforeEach(
6059
() => {
6160
cy.visit(getUrl({ prefix: '$', suffix: 'AUD', cursor: 'move' }, '6.66'));
62-
cy.reload();
6361
}
6462
);
6563

@@ -70,15 +68,46 @@ describe(
7068
cy.get('input').type('{leftArrow}');
7169
cy.get('input').type('9');
7270
cy.get('input').should('have.value', '$66,96AUD');
71+
}
72+
);
7373

74-
cy.get('input').type('{backspace}'.repeat(4));
74+
it(
75+
'shouldn\'t erase prefix',
76+
() => {
77+
cy.get('input').should('have.value', '$6,66AUD');
78+
cy.get('input').type('{backspace}'.repeat(8));
7579
cy.get('input').type('123');
7680
cy.get('input').should('have.value', '$1,23AUD');
7781
cy.get('input').type('{leftArrow}'.repeat(4));
7882
cy.get('input').type('2');
7983
cy.get('input').should('have.value', '$21,23AUD');
8084
}
8185
);
86+
87+
it(
88+
'should keep caret at correct position after inserted',
89+
() => {
90+
cy.get('input').should('have.value', '$6,66AUD');
91+
cy.get('input').type('{leftArrow}');
92+
cy.get('input').type('9');
93+
cy.get('input').should('have.value', '$66,96AUD');
94+
cy.get('input').type('{backspace}');
95+
cy.get('input').should('have.value', '$6,66AUD');
96+
}
97+
);
98+
99+
it(
100+
'should keep caret at correct position after clear',
101+
() => {
102+
cy.get('input').should('have.value', '$6,66AUD');
103+
cy.get('input').type('{leftArrow}');
104+
cy.get('input').type('9');
105+
cy.get('input').should('have.value', '$66,96AUD');
106+
cy.get('input').type('{leftArrow}');
107+
cy.get('input').type('{backspace}'.repeat(2));
108+
cy.get('input').should('have.value', '$0,96AUD');
109+
}
110+
);
82111
}
83112
);
84113
},
@@ -90,7 +119,6 @@ describe(
90119
beforeEach(
91120
() => {
92121
cy.visit(getUrl({ prefix: '$', suffix: 'AUD', cursor: 'move' }, '666.99'));
93-
cy.reload();
94122
}
95123
);
96124

@@ -200,3 +228,167 @@ describe(
200228
}
201229
);
202230

231+
232+
describe(
233+
'negative numbers',
234+
() => {
235+
describe(
236+
'should start with negative when allowed',
237+
() => {
238+
beforeEach(
239+
() => {
240+
cy.visit(getUrl({ allowNegative: true }, '-6.99'));
241+
}
242+
);
243+
244+
it(
245+
'should allow add negative sign',
246+
() => {
247+
cy.get('input').should('have.value', '-6,99');
248+
cy.get('input').type('-');
249+
cy.get('input').should('have.value', '-6,99');
250+
}
251+
);
252+
}
253+
);
254+
255+
describe(
256+
'should allow add and remove negative sign correctly',
257+
() => {
258+
beforeEach(
259+
() => {
260+
cy.visit(getUrl({ allowNegative: true, cursor: 'move' }));
261+
}
262+
);
263+
264+
it(
265+
'should allow add negative sign',
266+
() => {
267+
cy.get('input').should('have.value', '0,00');
268+
cy.get('input').type('-');
269+
cy.get('input').should('have.value', '-0,00');
270+
}
271+
);
272+
273+
it(
274+
'should allow add only one negative sign',
275+
() => {
276+
cy.get('input').should('have.value', '0,00');
277+
cy.get('input').type('----');
278+
cy.get('input').should('have.value', '-0,00');
279+
}
280+
);
281+
282+
it(
283+
'should allow write with negative sign',
284+
() => {
285+
cy.get('input').should('have.value', '0,00');
286+
cy.get('input').type('666');
287+
cy.get('input').type('-');
288+
cy.get('input').should('have.value', '-6,66');
289+
cy.get('input').type('963');
290+
cy.get('input').should('have.value', '-6.669,63');
291+
}
292+
);
293+
294+
it(
295+
'should keep caret at correct position after inserted',
296+
() => {
297+
cy.get('input').type('6-66');
298+
cy.get('input').should('have.value', '-6,66');
299+
cy.get('input').type('{leftArrow}');
300+
cy.get('input').type('9');
301+
cy.get('input').should('have.value', '-66,96');
302+
cy.get('input').type('{backspace}');
303+
cy.get('input').should('have.value', '-6,66');
304+
}
305+
);
306+
307+
it(
308+
'should keep caret at correct position after clear',
309+
() => {
310+
cy.get('input').type('6-66');
311+
cy.get('input').should('have.value', '-6,66');
312+
cy.get('input').type('{leftArrow}');
313+
cy.get('input').type('9');
314+
cy.get('input').should('have.value', '-66,96');
315+
cy.get('input').type('{leftArrow}');
316+
cy.get('input').type('{backspace}'.repeat(2));
317+
cy.get('input').should('have.value', '-0,96');
318+
}
319+
);
320+
}
321+
);
322+
323+
describe(
324+
'should allow add and remove negative sign correctly when it is after numbers',
325+
() => {
326+
beforeEach(
327+
() => {
328+
cy.visit(getUrl({ allowNegative: true, negativeSignAfter: true, cursor: 'move' }));
329+
}
330+
);
331+
332+
it(
333+
'should allow add negative sign',
334+
() => {
335+
cy.get('input').should('have.value', '0,00');
336+
cy.get('input').type('-');
337+
cy.get('input').should('have.value', '0,00-');
338+
}
339+
);
340+
341+
it(
342+
'should allow add only one negative sign',
343+
() => {
344+
cy.get('input').should('have.value', '0,00');
345+
cy.get('input').type('----');
346+
cy.get('input').should('have.value', '0,00-');
347+
}
348+
);
349+
350+
it(
351+
'should allow write with negative sign',
352+
() => {
353+
cy.get('input').should('have.value', '0,00');
354+
cy.get('input').type('666');
355+
cy.get('input').type('-');
356+
cy.get('input').should('have.value', '6,66-');
357+
cy.get('input').type('963');
358+
cy.get('input').should('have.value', '6.669,63-');
359+
}
360+
);
361+
362+
it(
363+
'should keep caret at correct position after inserted',
364+
() => {
365+
cy.get('input').type('6-66');
366+
cy.get('input').should('have.value', '6,66-');
367+
cy.get('input').type('{leftArrow}');
368+
cy.get('input').type('9');
369+
cy.get('input').should('have.value', '66,69-');
370+
cy.get('input').type('{backspace}');
371+
cy.get('input').should('have.value', '6,66-');
372+
}
373+
);
374+
375+
it(
376+
'should keep caret at correct position after clear',
377+
() => {
378+
cy.get('input').type('6-99'); // 6,99-|
379+
cy.get('input').should('have.value', '6,99-');
380+
cy.get('input').type('{leftArrow}'); // 6,99|-
381+
cy.get('input').type('3');
382+
cy.get('input').should('have.value', '69,93-');
383+
cy.get('input').type('{leftArrow}'.repeat(2)); // 69,|93-
384+
cy.get('input').type('{backspace}'.repeat(2)); // 0,|93-
385+
cy.get('input').should('have.value', '0,93-');
386+
cy.get('input').type('{rightArrow}'.repeat(3)); // 0,93-|
387+
cy.get('input').type('{backspace}');
388+
cy.get('input').should('have.value', '0,93');
389+
}
390+
);
391+
}
392+
);
393+
}
394+
);

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-mask-money",
3-
"version": "4.0.3",
3+
"version": "4.0.4",
44
"private": false,
55
"description": "Simple money mask developed with pure JavaScript. To run on Client Side and Server Side",
66
"types": "./lib/simple-mask-money.d.ts",

src/format-to-currency.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function formatToCurrency(
6060

6161
const isNegative = allowNegative && srtValue.includes('-');
6262

63-
const numberValue = formatToNumber(value, configuration);
63+
const numberValue = formatToNumber(value, configuration).toString().replace('-', '');
6464

6565
const [thousands, decimals] = numberValue.toString().split('.');
6666

src/format-to-number.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function formatToNumber(
3939
value: string | number,
4040
configuration?: OptionalSimpleMaskMoneyConfiguration,
4141
) {
42-
const { decimalSeparator } = getBaseConfiguration(configuration);
42+
const { decimalSeparator, allowNegative } = getBaseConfiguration(configuration);
4343

4444
const normalizeNumber = (n: number) =>
4545
n.toString().replace('.', decimalSeparator);
@@ -53,8 +53,10 @@ function formatToNumber(
5353
typeof value === 'number' ? normalizeNumber(value) : stringIsNumber(value);
5454

5555
const characteres = normalizedValue.split('');
56-
56+
5757
let result = '';
58+
59+
const isNegative = allowNegative && characteres.includes('-');
5860

5961
for (let character; (character = characteres.shift()); ) {
6062
if (!Number.isNaN(Number(character))) result += character;
@@ -63,7 +65,7 @@ function formatToNumber(
6365
result += '.';
6466
}
6567

66-
return parseFloat(result);
68+
return parseFloat(`${isNegative ? '-' : '' }${result}`);
6769
}
6870

6971
export default formatToNumber;

0 commit comments

Comments
 (0)