Skip to content

Instantly share code, notes, and snippets.

@grigs
Created September 19, 2019 00:39
Show Gist options
  • Save grigs/95a0ed20ba0ec463223fb7953a85ac47 to your computer and use it in GitHub Desktop.
Save grigs/95a0ed20ba0ec463223fb7953a85ac47 to your computer and use it in GitHub Desktop.
The section of Chipotle's checkout javascript that is doing form validation.
, function(e, t) {
var a = Math.abs;
(function() {
'use strict';
angular.module('ui.mask', []).value('uiMaskConfig', {
maskDefinitions: {
9: /\d/,
A: /[a-zA-Z]/,
"*": /[a-zA-Z0-9]/
},
clearOnBlur: !0,
clearOnBlurPlaceholder: !1,
escChar: '\\',
eventsToHandle: ['input', 'keyup', 'click', 'focus'],
addDefaultPlaceholder: !0,
allowInvalidValue: !1
}).provider('uiMask.Config', function() {
var e = {};
this.maskDefinitions = function(t) {
return e.maskDefinitions = t
}
,
this.clearOnBlur = function(t) {
return e.clearOnBlur = t
}
,
this.clearOnBlurPlaceholder = function(t) {
return e.clearOnBlurPlaceholder = t
}
,
this.eventsToHandle = function(t) {
return e.eventsToHandle = t
}
,
this.addDefaultPlaceholder = function(t) {
return e.addDefaultPlaceholder = t
}
,
this.allowInvalidValue = function(t) {
return e.allowInvalidValue = t
}
,
this.$get = ['uiMaskConfig', function(t) {
var a = t;
for (var r in e)
angular.isObject(e[r]) && !angular.isArray(e[r]) ? angular.extend(a[r], e[r]) : a[r] = e[r];
return a
}
]
}).directive('uiMask', ['uiMask.Config', function(e) {
function t(e) {
return e === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(e.type || e.href || ~e.tabIndex)
}
return {
priority: 100,
require: 'ngModel',
restrict: 'A',
compile: function r() {
var n = angular.copy(e);
return function e(r, o, l, i) {
function s(e) {
return angular.isDefined(e) ? (E(e),
!P) ? u() : (g(),
h(),
!0) : u()
}
function d(e) {
!e || (B = e,
P && !(0 === o.val().length && angular.isDefined(l.placeholder)) && o.val(y(b(o.val()))))
}
function c() {
return s(l.uiMask)
}
function m(e) {
return P ? (z = b(e || ''),
V = v(z),
i.$setValidity('mask', V),
z.length ? V || Q.allowInvalidValue ? y(z) : void 0 : void 0) : e
}
function p(e) {
return P ? (z = b(e || ''),
V = v(z),
i.$viewValue = z.length ? y(z) : '',
i.$setValidity('mask', V),
V || Q.allowInvalidValue) ? J ? i.$viewValue : z : void 0 : e
}
function u() {
return P = !1,
f(),
angular.isDefined(N) ? o.attr('placeholder', N) : o.removeAttr('placeholder'),
angular.isDefined(R) ? o.attr('maxlength', R) : o.removeAttr('maxlength'),
o.val(i.$modelValue),
i.$viewValue = i.$modelValue,
!1
}
function g() {
z = W = b(i.$modelValue || ''),
q = G = y(z),
V = v(z),
l.maxlength && o.attr('maxlength', 2 * j[j.length - 1]),
!N && Q.addDefaultPlaceholder && o.attr('placeholder', B);
for (var e = i.$modelValue, t = i.$formatters.length; t--; )
e = i.$formatters[t](e);
i.$viewValue = e || '',
i.$render()
}
function h() {
D || (o.bind('blur', C),
o.bind('mousedown mouseup', k),
o.bind('keydown', M),
o.bind(Q.eventsToHandle.join(' '), S),
D = !0)
}
function f() {
!D || (o.unbind('blur', C),
o.unbind('mousedown', k),
o.unbind('mouseup', k),
o.unbind('keydown', M),
o.unbind('input', S),
o.unbind('keyup', S),
o.unbind('click', S),
o.unbind('focus', S),
D = !1)
}
function v(e) {
return !e.length || e.length >= U
}
function b(e) {
var t = '', a = o[0], r = H.slice(), n = Y, i = n + O(a), l = '', s, d;
return e = e.toString(),
s = 0,
d = e.length - B.length,
angular.forEach(F, function(t) {
var a = t.position;
!(a >= n && a < i) && (a >= n && (a += d),
e.substring(a, a + t.value.length) === t.value && (l += e.slice(s, a),
s = a + t.value.length))
}),
e = l + e.slice(s),
angular.forEach(e.split(''), function(e) {
r.length && r[0].test(e) && (t += e,
r.shift())
}),
t
}
function y(e) {
var t = ''
, a = j.slice();
return angular.forEach(B.split(''), function(r, n) {
e.length && n === a[0] ? (t += e.charAt(0) || '_',
e = e.substr(1),
a.shift()) : t += r
}),
t
}
function $(e) {
var t = angular.isDefined(l.uiMaskPlaceholder) ? l.uiMaskPlaceholder : l.placeholder, a;
return angular.isDefined(t) && t[e] ? t[e] : (a = angular.isDefined(l.uiMaskPlaceholderChar) && l.uiMaskPlaceholderChar ? l.uiMaskPlaceholderChar : '_',
'space' === a.toLowerCase() ? ' ' : a[0])
}
function w() {
var e = B.split(''), t, a;
j && !isNaN(j[0]) && angular.forEach(j, function(t) {
e[t] = '_'
}),
t = e.join(''),
a = t.replace(/[_]+/g, '_').split('_'),
a = a.filter(function(e) {
return '' !== e
});
var r = 0;
return a.map(function(e) {
var a = t.indexOf(e, r);
return r = a + 1,
{
value: e,
position: a
}
})
}
function E(e) {
var t = 0;
if (j = [],
H = [],
B = '',
angular.isString(e)) {
U = 0;
var a = !1
, r = 0
, n = e.split('')
, o = !1;
angular.forEach(n, function(e, n) {
o ? (o = !1,
B += e,
t++) : Q.escChar === e ? o = !0 : Q.maskDefinitions[e] ? (j.push(t),
B += $(n - r),
H.push(Q.maskDefinitions[e]),
t++,
!a && U++,
a = !1) : '?' === e ? (a = !0,
r++) : (B += e,
t++)
})
}
j.push(j.slice().pop() + 1),
F = w(),
P = !!(1 < j.length)
}
function C() {
if ((Q.clearOnBlur || Q.clearOnBlurPlaceholder && 0 === z.length && l.placeholder) && (Y = 0,
K = 0,
(!V || 0 === z.length) && (q = '',
o.val(''),
r.$apply(function() {
!i.$pristine && i.$setViewValue('')
}))),
z !== Z) {
var e = o.val()
, t = '' === z && e && angular.isDefined(l.uiMaskPlaceholderChar) && 'space' === l.uiMaskPlaceholderChar;
t && o.val(''),
x(o[0]),
t && o.val(e)
}
Z = z
}
function x(e) {
var t;
angular.isFunction(window.Event) && !e.fireEvent ? (t = new Event('change',{
view: window,
bubbles: !0,
cancelable: !1
}),
e.dispatchEvent(t)) : 'createEvent'in document ? (t = document.createEvent('HTMLEvents'),
t.initEvent('change', !1, !0),
e.dispatchEvent(t)) : e.fireEvent && e.fireEvent('onchange')
}
function k(t) {
'mousedown' === t.type ? o.bind('mouseout', T) : o.unbind('mouseout', T)
}
function T() {
K = O(this),
o.unbind('mouseout', T)
}
function M(t) {
var e = 8 === t.which
, a = A(this) - 1 || 0
, r = 90 === t.which && t.ctrlKey;
if (e) {
for (; 0 <= a; ) {
if (_(a)) {
I(this, a + 1);
break
}
a--
}
X = -1 == a
}
r && (o.val(''),
t.preventDefault())
}
function S(t) {
t = t || {};
var e = t.which
, a = t.type;
if (16 !== e && 91 !== e) {
var n = o.val(), l = G, s = !1, d = b(n), c = W, m = A(this) || 0, p = Y || 0, u = m - p, g = j[0], h = j[d.length] || j.slice().shift(), f = K || 0, v = 0 < O(this), $ = 0 < f, w = n.length > l.length || f && n.length > l.length - f, E = n.length < l.length || f && n.length === l.length - f, C = 37 <= e && 40 >= e && t.shiftKey, x = 37 === e, k = 8 === e || 'keyup' !== a && E && -1 == u, T = 46 === e || 'keyup' !== a && E && 0 == u && !$, M = (x || k || 'click' === a) && m > g, S;
if (K = O(this),
!(C || v && ('click' === a || 'keyup' === a || 'focus' === a))) {
if (k && X)
return o.val(B),
r.$apply(function() {
i.$setViewValue('')
}),
void I(this, p);
if ('input' === a && E && !$ && d === c) {
for (; k && m > g && !_(m); )
m--;
for (; T && m < h && -1 === j.indexOf(m); )
m++;
var P = j.indexOf(m);
d = d.substring(0, P) + d.substring(P + 1),
d !== c && (s = !0)
}
for (S = y(d),
G = S,
W = d,
!s && n.length > S.length && (s = !0),
o.val(S),
s && r.$apply(function() {
i.$setViewValue(S)
}),
w && m <= g && (m = g + 1),
M && m--,
m = m > h ? h : m < g ? g : m; !_(m) && m > g && m < h; )
m += M ? -1 : 1;
(M && m < h || w && !_(p)) && m++,
Y = m,
I(this, m)
}
}
}
function _(e) {
return -1 < j.indexOf(e)
}
function A(e) {
if (!e)
return 0;
if (void 0 !== e.selectionStart)
return e.selectionStart;
if (document.selection && t(o[0])) {
e.focus();
var a = document.selection.createRange();
return a.moveStart('character', e.value ? -e.value.length : 0),
a.text.length
}
return 0
}
function I(e, a) {
if (!e)
return 0;
if (0 !== e.offsetWidth && 0 !== e.offsetHeight)
if (e.setSelectionRange)
t(o[0]) && (e.focus(),
e.setSelectionRange(a, a));
else if (e.createTextRange) {
var r = e.createTextRange();
r.collapse(!0),
r.moveEnd('character', a),
r.moveStart('character', a),
r.select()
}
}
function O(e) {
return e ? void 0 === e.selectionStart ? window.getSelection ? window.getSelection().toString().length : document.selection ? document.selection.createRange().text.length : 0 : e.selectionEnd - e.selectionStart : 0
}
var P = !1, D = !1, N = l.placeholder, R = l.maxlength, L = i.$isEmpty, j, H, B, F, U, z, q, V, G, W, Y, K, X;
i.$isEmpty = function(e) {
return P ? L(b(e || '')) : L(e)
}
;
var J = !1;
l.$observe('modelViewValue', function(e) {
'true' === e && (J = !0)
}),
l.$observe('allowInvalidValue', function(e) {
Q.allowInvalidValue = !('' !== e) || !!e,
m(i.$modelValue)
});
var Q = {};
l.uiOptions ? (Q = r.$eval('[' + l.uiOptions + ']'),
Q = angular.isObject(Q[0]) ? function(e, t) {
for (var a in e)
Object.prototype.hasOwnProperty.call(e, a) && (void 0 === t[a] ? t[a] = angular.copy(e[a]) : angular.isObject(t[a]) && !angular.isArray(t[a]) && (t[a] = angular.extend({}, e[a], t[a])));
return t
}(n, Q[0]) : n) : Q = n,
l.$observe('uiMask', s),
angular.isDefined(l.uiMaskPlaceholder) ? l.$observe('uiMaskPlaceholder', d) : l.$observe('placeholder', d),
angular.isDefined(l.uiMaskPlaceholderChar) && l.$observe('uiMaskPlaceholderChar', c),
i.$formatters.unshift(m),
i.$parsers.unshift(p);
var Z = o.val();
o.bind('mousedown mouseup', k),
!Array.prototype.indexOf && (Array.prototype.indexOf = function(e) {
if (null === this)
throw new TypeError;
var r = Object(this)
, t = r.length >>> 0;
if (0 == t)
return -1;
var o = 0;
if (1 < arguments.length && (o = +arguments[1],
o == o ? 0 != o && o != Infinity && o != -Infinity && (o = (0 < o || -1) * Math.floor(a(o))) : o = 0),
o >= t)
return -1;
for (var n = 0 <= o ? o : Math.max(t - a(o), 0); n < t; n++)
if (n in r && r[n] === e)
return n;
return -1
}
)
}
}
}
}
])
}
)()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment