Skip to content

Instantly share code, notes, and snippets.

@renoirb
Created January 28, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/22144e2116699f2ea081adfc4438e7d1 to your computer and use it in GitHub Desktop.
Save renoirb/22144e2116699f2ea081adfc4438e7d1 to your computer and use it in GitHub Desktop.
Vue copy-pasta
/**
* Copy-Pasta from Vue.js transpiled source (!).
*
* NOT TO USE, just to point out cool parts.
*
* https://github.com/vuejs/vue/blob/2.6/dist/vue.runtime.esm.js
*/
import {
extend,
isPrimitive,
makeMap,
isPlainObject,
isDef,
isUndef,
isObject,
isTrue,
isFalse,
} from './utils.js'
/**
* Runtime helper for v-once.
* Effectively it means marking the node as static with a unique key.
*/
function markOnce(tree, index, key) {
markStatic(tree, '__once__' + index + (key ? '_' + key : ''), true)
return tree
}
function markStatic(tree, key, isOnce) {
if (Array.isArray(tree)) {
for (var i = 0; i < tree.length; i++) {
if (tree[i] && typeof tree[i] !== 'string') {
markStaticNode(tree[i], key + '_' + i, isOnce)
}
}
} else {
markStaticNode(tree, key, isOnce)
}
}
function markStaticNode(node, key, isOnce) {
node.isStatic = true
node.key = key
node.isOnce = isOnce
}
/* ***************************************** */
/**
* Convert a value to a string that is actually rendered.
*/
function toString(val) {
return val == null
? ''
: typeof val === 'object'
? JSON.stringify(val, null, 2)
: String(val)
}
/**
* Convert an input value to a number for persistence.
* If the conversion fails, return original string.
*/
export function toNumber(val) {
var n = parseFloat(val)
return isNaN(n) ? val : n
}
/* ***************************************** */
/**
* Runtime helper for rendering v-for lists.
*/
function renderList(val, render) {
var ret, i, l, keys, key
if (Array.isArray(val) || typeof val === 'string') {
ret = new Array(val.length)
for (i = 0, l = val.length; i < l; i++) {
ret[i] = render(val[i], i)
}
} else if (typeof val === 'number') {
ret = new Array(val)
for (i = 0; i < val; i++) {
ret[i] = render(i + 1, i)
}
} else if (isObject(val)) {
keys = Object.keys(val)
ret = new Array(keys.length)
for (i = 0, l = keys.length; i < l; i++) {
key = keys[i]
ret[i] = render(val[key], key, i)
}
}
if (!isDef(ret)) {
ret = []
}
ret._isVList = true
return ret
}
/* ***************************************** */
/**
* Return the same value.
*/
export const identity = function(_) {
return _
}
function warn(a, b) {}
/* ***************************************** */
/**
* Check if two values are loosely equal - that is,
* if they are plain objects, do they have the same shape?
*/
function looseEqual(a, b) {
if (a === b) {
return true
}
var isObjectA = isObject(a)
var isObjectB = isObject(b)
if (isObjectA && isObjectB) {
try {
var isArrayA = Array.isArray(a)
var isArrayB = Array.isArray(b)
if (isArrayA && isArrayB) {
return (
a.length === b.length &&
a.every(function(e, i) {
return looseEqual(e, b[i])
})
)
} else if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime()
} else if (!isArrayA && !isArrayB) {
var keysA = Object.keys(a)
var keysB = Object.keys(b)
return (
keysA.length === keysB.length &&
keysA.every(function(key) {
return looseEqual(a[key], b[key])
})
)
} else {
/* istanbul ignore next */
return false
}
} catch (e) {
/* istanbul ignore next */
return false
}
} else if (!isObjectA && !isObjectB) {
return String(a) === String(b)
} else {
return false
}
}
/**
* Return the first index at which a loosely equal value can be
* found in the array (if value is a plain object, the array must
* contain an object of the same shape), or -1 if it is not present.
*/
function looseIndexOf(arr, val) {
for (var i = 0; i < arr.length; i++) {
if (looseEqual(arr[i], val)) {
return i
}
}
return -1
}
/* ***************************************** */
/**
* Runtime helper for merging v-bind="object" into a VNode's data.
*/
function bindObjectProps(data, tag, value, asProp, isSync) {
if (value) {
if (!isObject(value)) {
process.env.NODE_ENV !== 'production' &&
warn('v-bind without argument expects an Object or Array value', this)
} else {
if (Array.isArray(value)) {
value = toObject(value)
}
var hash
var loop = function(key) {
if (key === 'class' || key === 'style' || isReservedAttribute(key)) {
hash = data
} else {
var type = data.attrs && data.attrs.type
hash =
asProp || config.mustUseProp(tag, type, key)
? data.domProps || (data.domProps = {})
: data.attrs || (data.attrs = {})
}
var camelizedKey = camelize(key)
if (!(key in hash) && !(camelizedKey in hash)) {
hash[key] = value[key]
if (isSync) {
var on = data.on || (data.on = {})
on['update:' + camelizedKey] = function($event) {
value[key] = $event
}
}
}
}
for (var key in value) loop(key)
}
}
return data
}
/**
* Merge an Array of Objects into a single Object.
*/
function toObject(arr) {
var res = {}
for (var i = 0; i < arr.length; i++) {
if (arr[i]) {
extend(res, arr[i])
}
}
return res
}
/**
* Create a cached version of a pure function.
*/
export 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() : ''
})
})
/**
* Check if an attribute is a reserved attribute.
*/
var isReservedAttribute = makeMap('key,ref,slot,slot-scope,is')
/* ***************************************** */
function bindObjectListeners(data, value) {
if (value) {
if (!isPlainObject(value)) {
process.env.NODE_ENV !== 'production' &&
warn('v-on without argument expects an Object value', this)
} else {
var on = (data.on = data.on ? extend({}, data.on) : {})
for (var key in value) {
var existing = on[key]
var ours = value[key]
on[key] = existing ? [].concat(existing, ours) : ours
}
}
}
return data
}
/* ***************************************** */
export default function installRenderHelpers(target) {
target.markOnce = markOnce
target.toNumber = toNumber
target.toString = toString
target.renderList = renderList
target.looseEqual = looseEqual
target.looseIndexOf = looseIndexOf
target.bindObjectProps = bindObjectProps
target.bindObjectListeners = bindObjectListeners
target.issers = {
isPrimitive,
isPlainObject,
isDef,
isUndef,
isObject,
isTrue,
isFalse,
}
}
// @ts-nocheck
/**
* Copy-Pasta from Vue.js transpiled source (!).
*
* https://github.com/vuejs/vue/blob/2.6/dist/vue.runtime.esm.js
*/
/**
* Mix properties into target object.
*/
export function extend(to, _from) {
for (var key in _from) {
to[key] = _from[key]
}
return to
}
/**
* Make a map and return a function for checking if a key
* is in that map.
*/
export function makeMap(str, expectsLowerCase = false) {
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]
}
}
/**
* Check if value is primitive.
*/
export function isPrimitive(value) {
return (
typeof value === 'string' ||
typeof value === 'number' ||
// $flow-disable-line
typeof value === 'symbol' ||
typeof value === 'boolean'
)
}
/**
* Quick object check - this is primarily used to tell
* Objects from primitive values when we know the value
* is a JSON-compliant type.
*/
export function isObject(obj) {
return obj !== null && typeof obj === 'object'
}
// These helpers produce better VM code in JS engines due to their
// explicitness and function inlining.
export function isUndef(v) {
return v === undefined || v === null
}
export function isDef(v) {
return v !== undefined && v !== null
}
export function isTrue(v) {
return v === true
}
export function isFalse(v) {
return v === false
}
/**
* Get the raw type string of a value, e.g., [object Object].
*/
var _toString = Object.prototype.toString
/**
* Strict object type check. Only returns true
* for plain JavaScript objects.
*/
export function isPlainObject(obj) {
return _toString.call(obj) === '[object Object]'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment