Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cosimomeli committed Nov 16, 2016
0 parents commit 4c716ed
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
bower_components/
.idea
coverage
npm-debug.log
26 changes: 26 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"node": true,
"browser": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"maxdepth": 2,
"maxcomplexity": 10,
"globals": {
"angular": false
}
}
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
sudo: false
node_js:
- 0.12
- 4
- 5
- 6
before_script:
- npm run bower
after_success:
- cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js
51 changes: 51 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* License: MIT.
* Copyright (C) 2016, Cosimo Meli.
*/

'use strict';

module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);

grunt.initConfig({
karma: {
unit: {
configFile: 'karma.conf.js',
singleRun: true
}
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: [
'Gruntfile.js',
'angular-xregexp.js',
'tests.js'
]
},
uglify: {
dist: {
options: {
sourceMap: true
},
files: {
'angular-xregexp.min.js': 'angular-xregexp.js'
}
}
}
});

grunt.registerTask('test', [
'jshint',
'karma'
]);

grunt.registerTask('build', [
'jshint',
'uglify'
]);

grunt.registerTask('default', ['build']);
};
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2016 Cosimo Meli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
angular-xregexp
==============

XRegExp validator for AngularJS.
This library decorates ngPattern to use [XRegExp](http://xregexp.com/) for more complex regular expression.


Installation
------------

You can choose your preferred method of installation:
* Through bower: `bower install angular-xregexp --save`
* Through npm: `npm install angular-xregexp xregexp --save`
* Download from github: [angular-xregexp.min.js](https://raw.github.com/cosimomeli/angular-xregexp/master/angular-xregexp.min.js)

Usage
-----
Include both **xregexp-all.js** and **angular-xregexp.js** in your application.

```html
<script src="components/xregexp/xegexp-all.js"></script>
<script src="components/angular-xregexp/angular-xregexp.js"></script>
```

Add the module `angularXRegExp` as a dependency to your app module:

```js
var myapp = angular.module('myapp', ['angularXRegExp']);
```
Then just use `ng-pattern` as always.

License
----

Released under the terms of the [MIT License](LICENSE).
133 changes: 133 additions & 0 deletions angular-xregexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* angular-xregexp.js / v1.0.0 / (c) 2016 Cosimo Meli / MIT Licence */

'format amd';
/* global define */

(function () {
'use strict';

function requireXRegExp() {
try {
return require('xregexp'); // Using nw.js or browserify?
} catch (e) {
throw new Error('Please install XRegExp via npm. Please reference to: https://github.com/cosimomeli/angular-xregexp');
}
}

function angularXRegExp(angular, XRegExp) {

if (typeof XRegExp === 'undefined') {
if (typeof require === 'function') {
XRegExp = requireXRegExp();
} else {
throw new Error('XRegExp cannot be found by angular-xregexp! Please reference to: https://github.com/cosimomeli/angular-xregexp');
}
}

var NODE_TYPE_TEXT = 3;

/**
* @returns {string} Returns the string representation of the element.
*/
function startingTag(element) {
element = angular.element(element).clone();
try {
// turns out IE does not let you set .html() on elements which
// are not allowed to have children. So we just ignore it.
element.empty();
} catch (e) {
}
var elemHtml = angular.element('<div>').append(element).html();
try {
return element[0].nodeType === NODE_TYPE_TEXT ? angular.lowercase(elemHtml) :
elemHtml.match(/^(<[^>]+>)/)[1].replace(/^<([\w\-]+)/, function (match, nodeName) {
return '<' + angular.lowercase(nodeName);
});
} catch (e) {
return angular.lowercase(elemHtml);
}

}

/**
* New link function to replace the ngPattern one
*/
function newLink(scope, elm, attr, ctrl) {
if (!ctrl) {
return;
}

function toXRegExp(regex) {
var REGEX_STRING_REGEXP = /^\/(.+)\/([a-z]*)$/;
if (regex instanceof RegExp) {
regex = regex.toString();
}
var match = regex.match(REGEX_STRING_REGEXP);
if (match) {
return new XRegExp(match[1], match[2]);
}

}

var regexp, patternExp = toXRegExp(attr.ngPattern || attr.pattern);

attr.$observe('pattern', function (regex) {
if (angular.isString(regex) && regex.length > 0) {
regex = new XRegExp('^' + regex + '$');
} else if (regex instanceof RegExp) {
regex = toXRegExp(regex);
}

if (regex && !regex.test) {
throw angular.$$minErr('ngPattern')('noregexp',
'Expected {0} to be a RegExp but was {1}. Element: {2}', patternExp,
regex, startingTag(elm));
}

regexp = regex || undefined;
ctrl.$validate();
});

ctrl.$validators.pattern = function (modelValue, viewValue) {
// HTML5 pattern constraint validates the input value, so we validate the viewValue
return ctrl.$isEmpty(viewValue) || angular.isUndefined(regexp) || regexp.test(viewValue);
};
}

/**
* @ngdoc overview
* @name angularXRegExp
*
* @description
* angularXRegExp module provides XRegExp functionality for angular.js apps.
*/
angular.module('angularXRegExp', [])

/**
* @ngdoc object
* @name angularXRegExp.config
*/
.config(['$provide', function ($provide) {
$provide.decorator('ngPatternDirective', ['$delegate', function ($delegate) {
//Replace ngPattern's link function
$delegate[0].compile = function () {
return function () {
newLink.apply(this, arguments);
};
};
return $delegate;
}]);
}]);

return 'angularXRegExp';
}

var isElectron = window && window.process && window.process.type;
if (typeof define === 'function' && define.amd) {
define(['angular', 'xregexp'], angularXRegExp);
} else if (typeof module !== 'undefined' && module && module.exports && (typeof require === 'function') && !isElectron) {
module.exports = angularXRegExp(require('angular'), require('xregexp'));
} else {
angularXRegExp(angular, (typeof global !== 'undefined' ? global : window).XRegExp);
}
})();
2 changes: 2 additions & 0 deletions angular-xregexp.min.js

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

73 changes: 73 additions & 0 deletions angular-xregexp.min.js.map

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

21 changes: 21 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "angular-xregexp",
"description": "XRegExp validator for AngularJS",
"author": "Cosimo Meli",
"license": "MIT",
"homepage": "https://github.com/cosimomeli/angular-xregexp",
"main": "./angular-xregexp.js",
"ignore": [
],
"dependencies": {
"angular": "^1.2.0",
"xregexp": "3.x"
},
"devDependencies": {
"angular-mocks": "1.4.x"
},
"repository": {
"type": "git",
"url": "git://github.com/cosimomeli/angular-xregexp.git"
}
}
Loading

0 comments on commit 4c716ed

Please sign in to comment.