Skip to content

Commit 9cbe403

Browse files
committed
Regenerate assets
1 parent 8851a88 commit 9cbe403

13 files changed

+424
-96
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Cleave.js has a simple purpose: to help you format input text content automatica
1818
- CommonJS / AMD mode
1919
- ReactJS component
2020
- AngularJS directive (1.x)
21+
- ES Module
2122

2223
**TL;DR** [the demo page](http://nosir.github.io/cleave.js/)
2324

@@ -64,7 +65,7 @@ var cleave = new Cleave('.input-phone', {
6465
});
6566
```
6667

67-
> `.input-element` here is a unique DOM element. If you want to apply Cleave for multiple elements, you need to give different CSS selectors and apply to each of them, effectively, you might want to create individual instance by a loop, e.g. [loop solution](https://github.com/nosir/cleave.js/issues/138#issuecomment-268024840)
68+
> `.input-element` here is a unique DOM element. If you want to apply Cleave for multiple elements, you need to give different CSS selectors and apply to each of them, effectively, you might want to create individual instance by a loop, e.g. [loop solution](https://github.com/nosir/cleave.js/issues/138#issuecomment-268024840)
6869
6970
More examples: [the demo page](http://nosir.github.io/cleave.js/)
7071

@@ -84,17 +85,14 @@ require(['cleave.js/dist/cleave.min', 'cleave.js/dist/addons/cleave-phone.{count
8485
});
8586
```
8687

87-
#### ES Module (Rollup, WebPack)
88+
#### ES Module
8889
```js
90+
// Rollup, WebPack
8991
import Cleave from 'cleave.js';
90-
9192
var cleave = new Cleave(...)
92-
```
9393

94-
#### ES Module (Browser)
95-
```js
94+
// Browser
9695
import Cleave from 'node_modules/cleave.js/dist/cleave-esm.min.js';
97-
9896
var cleave = new Cleave(...)
9997
```
10098

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description":
44
"JavaScript library for formatting input text content when you are typing",
55
"keywords": ["cleave", "javascript", "html", "form", "input"],
6-
"version": "1.5.0",
6+
"version": "1.5.1",
77
"author": {
88
"name": "Max Huang",
99
"email": "[email protected]",

dist/cleave-angular.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cleave-esm.js

Lines changed: 146 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var NumeralFormatter = function (numeralDecimalMark,
66
numeralThousandsGroupStyle,
77
numeralPositiveOnly,
88
stripLeadingZeroes,
9+
prefix,
10+
signBeforePrefix,
911
delimiter) {
1012
var owner = this;
1113

@@ -15,6 +17,8 @@ var NumeralFormatter = function (numeralDecimalMark,
1517
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
1618
owner.numeralPositiveOnly = !!numeralPositiveOnly;
1719
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
20+
owner.prefix = (prefix || prefix === '') ? prefix : '';
21+
owner.signBeforePrefix = !!signBeforePrefix;
1822
owner.delimiter = (delimiter || delimiter === '') ? delimiter : ',';
1923
owner.delimiterRE = delimiter ? new RegExp('\\' + delimiter, 'g') : '';
2024
};
@@ -32,7 +36,7 @@ NumeralFormatter.prototype = {
3236
},
3337

3438
format: function (value) {
35-
var owner = this, parts, partInteger, partDecimal = '';
39+
var owner = this, parts, partSign, partSignAndPrefix, partInteger, partDecimal = '';
3640

3741
// strip alphabet letters
3842
value = value.replace(/[A-Za-z]/g, '')
@@ -60,6 +64,17 @@ NumeralFormatter.prototype = {
6064
value = value.replace(/^(-)?0+(?=\d)/, '$1');
6165
}
6266

67+
partSign = value.slice(0, 1) === '-' ? '-' : '';
68+
if (typeof owner.prefix != 'undefined') {
69+
if (owner.signBeforePrefix) {
70+
partSignAndPrefix = partSign + owner.prefix;
71+
} else {
72+
partSignAndPrefix = owner.prefix + partSign;
73+
}
74+
} else {
75+
partSignAndPrefix = partSign;
76+
}
77+
6378
partInteger = value;
6479

6580
if (value.indexOf(owner.numeralDecimalMark) >= 0) {
@@ -68,8 +83,12 @@ NumeralFormatter.prototype = {
6883
partDecimal = owner.numeralDecimalMark + parts[1].slice(0, owner.numeralDecimalScale);
6984
}
7085

86+
if(partSign === '-') {
87+
partInteger = partInteger.slice(1);
88+
}
89+
7190
if (owner.numeralIntegerScale > 0) {
72-
partInteger = partInteger.slice(0, owner.numeralIntegerScale + (value.slice(0, 1) === '-' ? 1 : 0));
91+
partInteger = partInteger.slice(0, owner.numeralIntegerScale);
7392
}
7493

7594
switch (owner.numeralThousandsGroupStyle) {
@@ -89,18 +108,34 @@ NumeralFormatter.prototype = {
89108
break;
90109
}
91110

92-
return partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '');
111+
return partSignAndPrefix + partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '');
93112
}
94113
};
95114

96115
var NumeralFormatter_1 = NumeralFormatter;
97116

98-
var DateFormatter = function (datePattern) {
117+
var DateFormatter = function (datePattern, dateMin, dateMax) {
99118
var owner = this;
100119

101120
owner.date = [];
102121
owner.blocks = [];
103122
owner.datePattern = datePattern;
123+
owner.dateMin = dateMin
124+
.split('-')
125+
.reverse()
126+
.map(function(x) {
127+
return parseInt(x, 10);
128+
});
129+
if (owner.dateMin.length === 2) owner.dateMin.unshift(0);
130+
131+
owner.dateMax = dateMax
132+
.split('-')
133+
.reverse()
134+
.map(function(x) {
135+
return parseInt(x, 10);
136+
});
137+
if (owner.dateMax.length === 2) owner.dateMax.unshift(0);
138+
104139
owner.initBlocks();
105140
};
106141

@@ -219,18 +254,77 @@ DateFormatter.prototype = {
219254
date = this.getFixedDate(day, month, year);
220255
}
221256

257+
// mm-yy || yy-mm
258+
if (value.length === 4 && (datePattern[0] === 'y' || datePattern[1] === 'y')) {
259+
monthStartIndex = datePattern[0] === 'm' ? 0 : 2;
260+
yearStartIndex = 2 - monthStartIndex;
261+
month = parseInt(value.slice(monthStartIndex, monthStartIndex + 2), 10);
262+
year = parseInt(value.slice(yearStartIndex, yearStartIndex + 2), 10);
263+
264+
fullYearDone = value.slice(yearStartIndex, yearStartIndex + 2).length === 2;
265+
266+
date = [0, month, year];
267+
}
268+
269+
// mm-yyyy || yyyy-mm
270+
if (value.length === 6 && (datePattern[0] === 'Y' || datePattern[1] === 'Y')) {
271+
monthStartIndex = datePattern[0] === 'm' ? 0 : 4;
272+
yearStartIndex = 2 - 0.5 * monthStartIndex;
273+
month = parseInt(value.slice(monthStartIndex, monthStartIndex + 2), 10);
274+
year = parseInt(value.slice(yearStartIndex, yearStartIndex + 4), 10);
275+
276+
fullYearDone = value.slice(yearStartIndex, yearStartIndex + 4).length === 4;
277+
278+
date = [0, month, year];
279+
}
280+
281+
date = owner.getRangeFixedDate(date);
222282
owner.date = date;
223283

224-
return date.length === 0 ? value : datePattern.reduce(function (previous, current) {
284+
var result = date.length === 0 ? value : datePattern.reduce(function (previous, current) {
225285
switch (current) {
226286
case 'd':
227-
return previous + owner.addLeadingZero(date[0]);
287+
return previous + (date[0] === 0 ? '' : owner.addLeadingZero(date[0]));
228288
case 'm':
229-
return previous + owner.addLeadingZero(date[1]);
230-
default:
231-
return previous + (fullYearDone ? owner.addLeadingZeroForYear(date[2]) : '');
289+
return previous + (date[1] === 0 ? '' : owner.addLeadingZero(date[1]));
290+
case 'y':
291+
return previous + (fullYearDone ? owner.addLeadingZeroForYear(date[2], false) : '');
292+
case 'Y':
293+
return previous + (fullYearDone ? owner.addLeadingZeroForYear(date[2], true) : '');
232294
}
233295
}, '');
296+
297+
return result;
298+
},
299+
300+
getRangeFixedDate: function (date) {
301+
var owner = this,
302+
datePattern = owner.datePattern,
303+
dateMin = owner.dateMin || [],
304+
dateMax = owner.dateMax || [];
305+
306+
if (!date.length || (dateMin.length < 3 && dateMax.length < 3)) return date;
307+
308+
if (
309+
datePattern.find(function(x) {
310+
return x.toLowerCase() === 'y';
311+
}) &&
312+
date[2] === 0
313+
) return date;
314+
315+
if (dateMax.length && (dateMax[2] < date[2] || (
316+
dateMax[2] === date[2] && (dateMax[1] < date[1] || (
317+
dateMax[1] === date[1] && dateMax[0] < date[0]
318+
))
319+
))) return dateMax;
320+
321+
if (dateMin.length && (dateMin[2] > date[2] || (
322+
dateMin[2] === date[2] && (dateMin[1] > date[1] || (
323+
dateMin[1] === date[1] && dateMin[0] > date[0]
324+
))
325+
))) return dateMin;
326+
327+
return date;
234328
},
235329

236330
getFixedDate: function (day, month, year) {
@@ -253,8 +347,12 @@ DateFormatter.prototype = {
253347
return (number < 10 ? '0' : '') + number;
254348
},
255349

256-
addLeadingZeroForYear: function (number) {
257-
return (number < 10 ? '000' : (number < 100 ? '00' : (number < 1000 ? '0' : ''))) + number;
350+
addLeadingZeroForYear: function (number, fullYearMode) {
351+
if (fullYearMode) {
352+
return (number < 10 ? '000' : (number < 100 ? '00' : (number < 1000 ? '0' : ''))) + number;
353+
}
354+
355+
return (number < 10 ? '0' : '') + number;
258356
}
259357
};
260358

@@ -511,8 +609,7 @@ var CreditCardDetector = {
511609
visa: [4, 4, 4, 4],
512610
mir: [4, 4, 4, 4],
513611
unionPay: [4, 4, 4, 4],
514-
general: [4, 4, 4, 4],
515-
generalStrict: [4, 4, 4, 7]
612+
general: [4, 4, 4, 4]
516613
},
517614

518615
re: {
@@ -556,6 +653,14 @@ var CreditCardDetector = {
556653
unionPay: /^62\d{0,14}/
557654
},
558655

656+
getStrictBlocks: function (block) {
657+
var total = block.reduce(function (prev, current) {
658+
return prev + current;
659+
}, 0);
660+
661+
return block.concat(19 - total);
662+
},
663+
559664
getInfo: function (value, strictMode) {
560665
var blocks = CreditCardDetector.blocks,
561666
re = CreditCardDetector.re;
@@ -568,24 +673,17 @@ var CreditCardDetector = {
568673

569674
for (var key in re) {
570675
if (re[key].test(value)) {
571-
var block;
572-
573-
if (strictMode) {
574-
block = blocks.generalStrict;
575-
} else {
576-
block = blocks[key];
577-
}
578-
676+
var matchedBlocks = blocks[key];
579677
return {
580678
type: key,
581-
blocks: block
679+
blocks: strictMode ? this.getStrictBlocks(matchedBlocks) : matchedBlocks
582680
};
583681
}
584682
}
585683

586684
return {
587-
type: 'unknown',
588-
blocks: strictMode ? blocks.generalStrict : blocks.general
685+
type: 'unknown',
686+
blocks: strictMode ? this.getStrictBlocks(blocks.general) : blocks.general
589687
};
590688
}
591689
};
@@ -778,6 +876,18 @@ var Util = {
778876
}, 1);
779877
},
780878

879+
// Check if input field is fully selected
880+
checkFullSelection: function(value) {
881+
try {
882+
var selection = window.getSelection() || document.getSelection() || {};
883+
return selection.toString().length === value.length;
884+
} catch (ex) {
885+
// Ignore
886+
}
887+
888+
return false;
889+
},
890+
781891
setSelection: function (element, position, doc) {
782892
if (element !== this.getActiveElement(doc)) {
783893
return;
@@ -862,6 +972,8 @@ var DefaultProperties = {
862972
// date
863973
target.date = !!opts.date;
864974
target.datePattern = opts.datePattern || ['d', 'm', 'Y'];
975+
target.dateMin = opts.dateMin || '';
976+
target.dateMax = opts.dateMax || '';
865977
target.dateFormatter = {};
866978

867979
// numeral
@@ -872,6 +984,7 @@ var DefaultProperties = {
872984
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
873985
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
874986
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
987+
target.signBeforePrefix = !!opts.signBeforePrefix;
875988

876989
// others
877990
target.numericOnly = target.creditCard || target.date || !!opts.numericOnly;
@@ -1014,6 +1127,8 @@ Cleave.prototype = {
10141127
pps.numeralThousandsGroupStyle,
10151128
pps.numeralPositiveOnly,
10161129
pps.stripLeadingZeroes,
1130+
pps.prefix,
1131+
pps.signBeforePrefix,
10171132
pps.delimiter
10181133
);
10191134
},
@@ -1038,7 +1153,7 @@ Cleave.prototype = {
10381153
return;
10391154
}
10401155

1041-
pps.dateFormatter = new Cleave.DateFormatter(pps.datePattern);
1156+
pps.dateFormatter = new Cleave.DateFormatter(pps.datePattern, pps.dateMin, pps.dateMax);
10421157
pps.blocks = pps.dateFormatter.getBlocks();
10431158
pps.blocksLength = pps.blocks.length;
10441159
pps.maxLength = Cleave.Util.getMaxLength(pps.blocks);
@@ -1101,11 +1216,13 @@ Cleave.prototype = {
11011216
},
11021217

11031218
onCut: function (e) {
1219+
if (!Cleave.Util.checkFullSelection(this.element.value)) return;
11041220
this.copyClipboardData(e);
11051221
this.onInput('');
11061222
},
11071223

11081224
onCopy: function (e) {
1225+
if (!Cleave.Util.checkFullSelection(this.element.value)) return;
11091226
this.copyClipboardData(e);
11101227
},
11111228

@@ -1163,8 +1280,10 @@ Cleave.prototype = {
11631280

11641281
// numeral formatter
11651282
if (pps.numeral) {
1166-
if (pps.prefix && (!pps.noImmediatePrefix || value.length)) {
1167-
pps.result = pps.prefix + pps.numeralFormatter.format(value);
1283+
// Do not show prefix when noImmediatePrefix is specified
1284+
// This mostly because we need to show user the native input placeholder
1285+
if (pps.prefix && pps.noImmediatePrefix && value.length === 0) {
1286+
pps.result = '';
11681287
} else {
11691288
pps.result = pps.numeralFormatter.format(value);
11701289
}

dist/cleave-esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)