Skip to content

Commit

Permalink
Regenerate assets
Browse files Browse the repository at this point in the history
  • Loading branch information
nosir committed Jun 29, 2019
1 parent 8851a88 commit 9cbe403
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 96 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Cleave.js has a simple purpose: to help you format input text content automatica
- CommonJS / AMD mode
- ReactJS component
- AngularJS directive (1.x)
- ES Module

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

Expand Down Expand Up @@ -64,7 +65,7 @@ var cleave = new Cleave('.input-phone', {
});
```

> `.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)
> `.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)
More examples: [the demo page](http://nosir.github.io/cleave.js/)

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

#### ES Module (Rollup, WebPack)
#### ES Module
```js
// Rollup, WebPack
import Cleave from 'cleave.js';

var cleave = new Cleave(...)
```

#### ES Module (Browser)
```js
// Browser
import Cleave from 'node_modules/cleave.js/dist/cleave-esm.min.js';

var cleave = new Cleave(...)
```

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description":
"JavaScript library for formatting input text content when you are typing",
"keywords": ["cleave", "javascript", "html", "form", "input"],
"version": "1.5.0",
"version": "1.5.1",
"author": {
"name": "Max Huang",
"email": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion dist/cleave-angular.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

173 changes: 146 additions & 27 deletions dist/cleave-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var NumeralFormatter = function (numeralDecimalMark,
numeralThousandsGroupStyle,
numeralPositiveOnly,
stripLeadingZeroes,
prefix,
signBeforePrefix,
delimiter) {
var owner = this;

Expand All @@ -15,6 +17,8 @@ var NumeralFormatter = function (numeralDecimalMark,
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
owner.prefix = (prefix || prefix === '') ? prefix : '';
owner.signBeforePrefix = !!signBeforePrefix;
owner.delimiter = (delimiter || delimiter === '') ? delimiter : ',';
owner.delimiterRE = delimiter ? new RegExp('\\' + delimiter, 'g') : '';
};
Expand All @@ -32,7 +36,7 @@ NumeralFormatter.prototype = {
},

format: function (value) {
var owner = this, parts, partInteger, partDecimal = '';
var owner = this, parts, partSign, partSignAndPrefix, partInteger, partDecimal = '';

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

partSign = value.slice(0, 1) === '-' ? '-' : '';
if (typeof owner.prefix != 'undefined') {
if (owner.signBeforePrefix) {
partSignAndPrefix = partSign + owner.prefix;
} else {
partSignAndPrefix = owner.prefix + partSign;
}
} else {
partSignAndPrefix = partSign;
}

partInteger = value;

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

if(partSign === '-') {
partInteger = partInteger.slice(1);
}

if (owner.numeralIntegerScale > 0) {
partInteger = partInteger.slice(0, owner.numeralIntegerScale + (value.slice(0, 1) === '-' ? 1 : 0));
partInteger = partInteger.slice(0, owner.numeralIntegerScale);
}

switch (owner.numeralThousandsGroupStyle) {
Expand All @@ -89,18 +108,34 @@ NumeralFormatter.prototype = {
break;
}

return partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '');
return partSignAndPrefix + partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '');
}
};

var NumeralFormatter_1 = NumeralFormatter;

var DateFormatter = function (datePattern) {
var DateFormatter = function (datePattern, dateMin, dateMax) {
var owner = this;

owner.date = [];
owner.blocks = [];
owner.datePattern = datePattern;
owner.dateMin = dateMin
.split('-')
.reverse()
.map(function(x) {
return parseInt(x, 10);
});
if (owner.dateMin.length === 2) owner.dateMin.unshift(0);

owner.dateMax = dateMax
.split('-')
.reverse()
.map(function(x) {
return parseInt(x, 10);
});
if (owner.dateMax.length === 2) owner.dateMax.unshift(0);

owner.initBlocks();
};

Expand Down Expand Up @@ -219,18 +254,77 @@ DateFormatter.prototype = {
date = this.getFixedDate(day, month, year);
}

// mm-yy || yy-mm
if (value.length === 4 && (datePattern[0] === 'y' || datePattern[1] === 'y')) {
monthStartIndex = datePattern[0] === 'm' ? 0 : 2;
yearStartIndex = 2 - monthStartIndex;
month = parseInt(value.slice(monthStartIndex, monthStartIndex + 2), 10);
year = parseInt(value.slice(yearStartIndex, yearStartIndex + 2), 10);

fullYearDone = value.slice(yearStartIndex, yearStartIndex + 2).length === 2;

date = [0, month, year];
}

// mm-yyyy || yyyy-mm
if (value.length === 6 && (datePattern[0] === 'Y' || datePattern[1] === 'Y')) {
monthStartIndex = datePattern[0] === 'm' ? 0 : 4;
yearStartIndex = 2 - 0.5 * monthStartIndex;
month = parseInt(value.slice(monthStartIndex, monthStartIndex + 2), 10);
year = parseInt(value.slice(yearStartIndex, yearStartIndex + 4), 10);

fullYearDone = value.slice(yearStartIndex, yearStartIndex + 4).length === 4;

date = [0, month, year];
}

date = owner.getRangeFixedDate(date);
owner.date = date;

return date.length === 0 ? value : datePattern.reduce(function (previous, current) {
var result = date.length === 0 ? value : datePattern.reduce(function (previous, current) {
switch (current) {
case 'd':
return previous + owner.addLeadingZero(date[0]);
return previous + (date[0] === 0 ? '' : owner.addLeadingZero(date[0]));
case 'm':
return previous + owner.addLeadingZero(date[1]);
default:
return previous + (fullYearDone ? owner.addLeadingZeroForYear(date[2]) : '');
return previous + (date[1] === 0 ? '' : owner.addLeadingZero(date[1]));
case 'y':
return previous + (fullYearDone ? owner.addLeadingZeroForYear(date[2], false) : '');
case 'Y':
return previous + (fullYearDone ? owner.addLeadingZeroForYear(date[2], true) : '');
}
}, '');

return result;
},

getRangeFixedDate: function (date) {
var owner = this,
datePattern = owner.datePattern,
dateMin = owner.dateMin || [],
dateMax = owner.dateMax || [];

if (!date.length || (dateMin.length < 3 && dateMax.length < 3)) return date;

if (
datePattern.find(function(x) {
return x.toLowerCase() === 'y';
}) &&
date[2] === 0
) return date;

if (dateMax.length && (dateMax[2] < date[2] || (
dateMax[2] === date[2] && (dateMax[1] < date[1] || (
dateMax[1] === date[1] && dateMax[0] < date[0]
))
))) return dateMax;

if (dateMin.length && (dateMin[2] > date[2] || (
dateMin[2] === date[2] && (dateMin[1] > date[1] || (
dateMin[1] === date[1] && dateMin[0] > date[0]
))
))) return dateMin;

return date;
},

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

addLeadingZeroForYear: function (number) {
return (number < 10 ? '000' : (number < 100 ? '00' : (number < 1000 ? '0' : ''))) + number;
addLeadingZeroForYear: function (number, fullYearMode) {
if (fullYearMode) {
return (number < 10 ? '000' : (number < 100 ? '00' : (number < 1000 ? '0' : ''))) + number;
}

return (number < 10 ? '0' : '') + number;
}
};

Expand Down Expand Up @@ -511,8 +609,7 @@ var CreditCardDetector = {
visa: [4, 4, 4, 4],
mir: [4, 4, 4, 4],
unionPay: [4, 4, 4, 4],
general: [4, 4, 4, 4],
generalStrict: [4, 4, 4, 7]
general: [4, 4, 4, 4]
},

re: {
Expand Down Expand Up @@ -556,6 +653,14 @@ var CreditCardDetector = {
unionPay: /^62\d{0,14}/
},

getStrictBlocks: function (block) {
var total = block.reduce(function (prev, current) {
return prev + current;
}, 0);

return block.concat(19 - total);
},

getInfo: function (value, strictMode) {
var blocks = CreditCardDetector.blocks,
re = CreditCardDetector.re;
Expand All @@ -568,24 +673,17 @@ var CreditCardDetector = {

for (var key in re) {
if (re[key].test(value)) {
var block;

if (strictMode) {
block = blocks.generalStrict;
} else {
block = blocks[key];
}

var matchedBlocks = blocks[key];
return {
type: key,
blocks: block
blocks: strictMode ? this.getStrictBlocks(matchedBlocks) : matchedBlocks
};
}
}

return {
type: 'unknown',
blocks: strictMode ? blocks.generalStrict : blocks.general
type: 'unknown',
blocks: strictMode ? this.getStrictBlocks(blocks.general) : blocks.general
};
}
};
Expand Down Expand Up @@ -778,6 +876,18 @@ var Util = {
}, 1);
},

// Check if input field is fully selected
checkFullSelection: function(value) {
try {
var selection = window.getSelection() || document.getSelection() || {};
return selection.toString().length === value.length;
} catch (ex) {
// Ignore
}

return false;
},

setSelection: function (element, position, doc) {
if (element !== this.getActiveElement(doc)) {
return;
Expand Down Expand Up @@ -862,6 +972,8 @@ var DefaultProperties = {
// date
target.date = !!opts.date;
target.datePattern = opts.datePattern || ['d', 'm', 'Y'];
target.dateMin = opts.dateMin || '';
target.dateMax = opts.dateMax || '';
target.dateFormatter = {};

// numeral
Expand All @@ -872,6 +984,7 @@ var DefaultProperties = {
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
target.signBeforePrefix = !!opts.signBeforePrefix;

// others
target.numericOnly = target.creditCard || target.date || !!opts.numericOnly;
Expand Down Expand Up @@ -1014,6 +1127,8 @@ Cleave.prototype = {
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
pps.stripLeadingZeroes,
pps.prefix,
pps.signBeforePrefix,
pps.delimiter
);
},
Expand All @@ -1038,7 +1153,7 @@ Cleave.prototype = {
return;
}

pps.dateFormatter = new Cleave.DateFormatter(pps.datePattern);
pps.dateFormatter = new Cleave.DateFormatter(pps.datePattern, pps.dateMin, pps.dateMax);
pps.blocks = pps.dateFormatter.getBlocks();
pps.blocksLength = pps.blocks.length;
pps.maxLength = Cleave.Util.getMaxLength(pps.blocks);
Expand Down Expand Up @@ -1101,11 +1216,13 @@ Cleave.prototype = {
},

onCut: function (e) {
if (!Cleave.Util.checkFullSelection(this.element.value)) return;
this.copyClipboardData(e);
this.onInput('');
},

onCopy: function (e) {
if (!Cleave.Util.checkFullSelection(this.element.value)) return;
this.copyClipboardData(e);
},

Expand Down Expand Up @@ -1163,8 +1280,10 @@ Cleave.prototype = {

// numeral formatter
if (pps.numeral) {
if (pps.prefix && (!pps.noImmediatePrefix || value.length)) {
pps.result = pps.prefix + pps.numeralFormatter.format(value);
// Do not show prefix when noImmediatePrefix is specified
// This mostly because we need to show user the native input placeholder
if (pps.prefix && pps.noImmediatePrefix && value.length === 0) {
pps.result = '';
} else {
pps.result = pps.numeralFormatter.format(value);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/cleave-esm.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 9cbe403

Please sign in to comment.