Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from shrpne/feature-rollup
Browse files Browse the repository at this point in the history
Add minified version, build with rollup
  • Loading branch information
probil authored Jul 13, 2017
2 parents 2561bf3 + 36e1aee commit e85d063
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 170 deletions.
15 changes: 12 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["transform-runtime"],
"comments": false
"presets": [
["es2015", {
"modules": false
}],
],
"comments": false,
"env": {
"test": {
"presets": ["es2015"],
"plugins": ["transform-runtime", "syntax-dynamic-import"],
}
}
}
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ This version requires Vue 2.X. If you are looking for Vue 1.X, [check it here](h

`npm install v-mask`

## Usage
## Initialization

As a plugin
ES2015

```javascript
import Vue from 'vue'
import Vue from 'vue'

// As a plugin
import VueMask from 'v-mask'
Vue.use(VueMask);
```

As a directive

```javascript
import Vue from 'vue'
// As a directive
import { VueMaskDirective } from 'v-mask'
Vue.directive('mask', VueMaskDirective);
```

Now you are ready to use it in the code!
UMD

```html
<script src="vue.min.js"></script>
<script src="v-mask.min.js"></script>
<script>
// As a plugin
Vue.use(VueMask.VueMaskPlugin);
// As a directive
Vue.directive('mask', VueMask.VueMaskDirective);
</script>
```

## Usage

```html
<input type="text" v-mask="'####-##'" v-model="myInputModel">
Expand Down
22 changes: 22 additions & 0 deletions build/rollup.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';


export default {
entry: 'src/index.js',
plugins: [
nodeResolve(),
babel(),
],
targets: [
{
format: 'umd',
moduleName: 'VueMask',
dest: 'dist/v-mask.js',
},
{
format: 'es',
dest: 'dist/v-mask.esm.js',
},
],
}
16 changes: 16 additions & 0 deletions build/rollup.conf.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import nodeResolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import uglify from 'rollup-plugin-uglify';


export default {
entry: 'src/index.js',
plugins: [
nodeResolve(),
babel(),
uglify(),
],
format: 'umd',
moduleName: 'VueMask',
dest: 'dist/v-mask.min.js',
}
76 changes: 0 additions & 76 deletions dist/format.js

This file was deleted.

64 changes: 0 additions & 64 deletions dist/index.js

This file was deleted.

10 changes: 0 additions & 10 deletions dist/utils/index.js

This file was deleted.

125 changes: 125 additions & 0 deletions dist/v-mask.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
var format = function (data, mask) {
if (!mask) return data;

var maskStartRegExp = /^([^#ANX]+)/;

if (data.length == 1 && maskStartRegExp.test(mask)) {
data = maskStartRegExp.exec(mask)[0] + data;
}

var text = '';

var cOffset = 0;

for (var i = 0; i < mask.length; i++) {
var m = mask.charAt(i);
switch (m) {
case '#':
break;
case 'A':
break;
case '?':
break;
case 'N':
break;
case 'X':
break;
default:
data = data.replace(m, '');
}
}
for (var _i = 0, x = 1; x && _i < mask.length; ++_i) {
var c = data.charAt(_i - cOffset);
var _m = mask.charAt(_i);

switch (_m) {
case '#':
if (/\d/.test(c)) {
text += c;
} else {
x = 0;
}break;
case 'A':
if (/[a-z]/i.test(c)) {
text += c;
} else {
x = 0;
}break;
case 'N':
if (/[a-z0-9]/i.test(c)) {
text += c;
} else {
x = 0;
}break;

case '?':
cOffset++;break;
case 'X':
text += c;break;
default:
text += _m;

if (c && c !== _m) {
data = ' ' + data;
}

break;
}
}
return text;
};

var trigger = function trigger(el, type) {
var e = document.createEvent('HTMLEvents');
e.initEvent(type, true, true);
el.dispatchEvent(e);
};

function updateValue(el) {
var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var value = el.value,
_el$dataset = el.dataset,
_el$dataset$previousV = _el$dataset.previousValue,
previousValue = _el$dataset$previousV === undefined ? "" : _el$dataset$previousV,
mask = _el$dataset.mask;


if (force || value && value !== previousValue && value.length > previousValue.length) {
el.value = format(value, mask);
trigger(el, 'input');
}

el.dataset.previousValue = value;
}

function updateMask(el, mask) {
el.dataset.mask = mask;
}

var VueMaskDirective = {
bind: function bind(el, _ref) {
var value = _ref.value;

updateMask(el, value);
updateValue(el);
},
componentUpdated: function componentUpdated(el, _ref2) {
var value = _ref2.value,
oldValue = _ref2.oldValue;


var isMaskChanged = value !== oldValue;

if (isMaskChanged) {
updateMask(el, value);
}

updateValue(el, isMaskChanged);
}
};

var VueMaskPlugin = function VueMaskPlugin(Vue) {
Vue.directive('mask', VueMaskDirective);
};

export { VueMaskPlugin, VueMaskDirective };export default VueMaskPlugin;
Loading

0 comments on commit e85d063

Please sign in to comment.