Last active
December 13, 2019 07:00
-
-
Save lilywang711/ca81c8b18310e2c8fdbb9dca5b15cc56 to your computer and use it in GitHub Desktop.
util functions in vue/packages/vue-template-compiler/build.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Define a property. | |
| */ | |
| function def (obj, key, val, enumerable) { | |
| Object.defineProperty(obj, key, { | |
| value: val, | |
| enumerable: !!enumerable, | |
| writable: true, | |
| configurable: true | |
| }); | |
| } | |
| // explicitness and function inlining. | |
| function isUndef (v) { | |
| return v === undefined || v === null | |
| } | |
| /** | |
| * Check if value is primitive. | |
| */ | |
| function isPrimitive (value) { | |
| return ( | |
| typeof value === 'string' || | |
| typeof value === 'number' || | |
| typeof value === 'symbol' || | |
| typeof value === 'boolean' | |
| ) | |
| } | |
| var _toString = Object.prototype.toString; | |
| function toRawType (value) { | |
| return _toString.call(value).slice(8, -1) | |
| } | |
| /** | |
| * Strict object type check. Only returns true | |
| * for plain JavaScript objects. | |
| */ | |
| function isPlainObject (obj) { | |
| return _toString.call(obj) === '[object Object]' | |
| } | |
| /** | |
| * Make a map and return a function for checking if a key | |
| * is in that map. | |
| */ | |
| function makeMap ( | |
| str, | |
| expectsLowerCase | |
| ) { | |
| var map = Object.create(null); | |
| var list = str.split(','); | |
| for (var i = 0; i < list.length; i++) { | |
| map[list[i]] = true; | |
| } | |
| return expectsLowerCase | |
| ? function (val) { return map[val.toLowerCase()]; } | |
| : function (val) { return map[val]; } | |
| } | |
| /** | |
| * Remove an item from an array. | |
| */ | |
| function remove (arr, item) { | |
| if (arr.length) { | |
| var index = arr.indexOf(item); | |
| if (index > -1) { | |
| return arr.splice(index, 1) | |
| } | |
| } | |
| } | |
| /** | |
| * Check whether an object has the property. | |
| */ | |
| var hasOwnProperty = Object.prototype.hasOwnProperty; | |
| function hasOwn (obj, key) { | |
| return hasOwnProperty.call(obj, key) | |
| } | |
| /** | |
| * Create a cached version of a pure function. | |
| */ | |
| function cached (fn) { | |
| var cache = Object.create(null); | |
| return (function cachedFn (str) { | |
| var hit = cache[str]; | |
| return hit || (cache[str] = fn(str)) | |
| }) | |
| } | |
| /** | |
| * Camelize a hyphen-delimited string. | |
| */ | |
| var camelizeRE = /-(\w)/g; | |
| var camelize = cached(function (str) { | |
| return str.replace(camelizeRE, function (_, c) { return c ? c.toUpperCase() : ''; }) | |
| }); | |
| /** | |
| * Hyphenate a camelCase string. | |
| */ | |
| var hyphenateRE = /\B([A-Z])/g; | |
| var hyphenate = cached(function (str) { | |
| return str.replace(hyphenateRE, '-$1').toLowerCase() | |
| }); | |
| /** | |
| * Return the same value. | |
| */ | |
| var identity = function (_) { return _; }; | |
| /** | |
| * Simple bind polyfill for environments that do not support it, | |
| * e.g., PhantomJS 1.x. Technically, we don't need this anymore | |
| * since native bind is now performant enough in most browsers. | |
| * But removing it would mean breaking code that was able to run in | |
| * PhantomJS 1.x, so this must be kept for backward compatibility. | |
| */ | |
| /* istanbul ignore next */ | |
| function polyfillBind (fn, ctx) { | |
| function boundFn (a) { | |
| var l = arguments.length; | |
| return l | |
| ? l > 1 | |
| ? fn.apply(ctx, arguments) | |
| : fn.call(ctx, a) | |
| : fn.call(ctx) | |
| } | |
| boundFn._length = fn.length; | |
| return boundFn | |
| } | |
| function nativeBind (fn, ctx) { | |
| return fn.bind(ctx) | |
| } | |
| var bind = Function.prototype.bind | |
| ? nativeBind | |
| : polyfillBind; | |
| /** | |
| * Mix properties into target object. | |
| */ | |
| function extend (to, _from) { | |
| for (var key in _from) { | |
| to[key] = _from[key]; | |
| } | |
| return to | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment