Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@
"access": "public"
},
"sideEffects": false,
"type": "module"
"type": "module",
"dependencies": {
"@vue/shared": "^3.5.22"
}
}
122 changes: 9 additions & 113 deletions packages/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
* Original Utilities
* written by kazuya kawaguchi
*/

/* eslint-disable vue/prefer-import-from-vue */
export * from '@vue/shared'
export { extend as assign } from '@vue/shared'
import { isObject, isPlainObject, hasOwn as vueHasOwn } from '@vue/shared'
export const inBrowser: boolean = typeof window !== 'undefined'

// wrap utility to prevent narrowing key to never
export const hasOwn = (val: object, key: string | symbol): boolean => vueHasOwn(val, key)

export let mark: (tag: string) => void | undefined
export let measure: (name: string, startTag: string, endTag: string) => void | undefined

Expand Down Expand Up @@ -32,7 +38,7 @@ if (__DEV__) {

const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g

/* eslint-disable */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function format(message: string, ...args: any): string {
if (args.length === 1 && isObject(args[0])) {
args = args[0]
Expand All @@ -41,7 +47,7 @@ export function format(message: string, ...args: any): string {
args = {}
}
return message.replace(RE_ARGS, (match: string, identifier: string): string => {
return args.hasOwnProperty(identifier) ? args[identifier] : ''
return hasOwn(args, identifier) ? args[identifier] : ''
})
}

Expand All @@ -59,124 +65,14 @@ export const friendlyJSONstringify = (json: unknown): string =>

export const isNumber = (val: unknown): val is number => typeof val === 'number' && isFinite(val)

export const isDate = (val: unknown): val is Date => toTypeString(val) === '[object Date]'

export const isRegExp = (val: unknown): val is RegExp => toTypeString(val) === '[object RegExp]'

export const isEmptyObject = (val: unknown): val is boolean =>
isPlainObject(val) && Object.keys(val).length === 0

export const assign: typeof Object.assign = Object.assign

const _create = Object.create
export const create = (obj: object | null = null): object => _create(obj)

let _globalThis: any
export const getGlobalThis = (): any => {
// prettier-ignore
return (
_globalThis ||
(_globalThis =
typeof globalThis !== 'undefined'
? globalThis
: typeof self !== 'undefined'
? self
: typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: create())
)
}

export function escapeHtml(rawText: string): string {
return rawText
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}

const hasOwnProperty = Object.prototype.hasOwnProperty
export function hasOwn(obj: object | Array<any>, key: string): boolean {
return hasOwnProperty.call(obj, key)
}

/* eslint-enable */

/**
* Useful Utilities By Evan you
* Modified by kazuya kawaguchi
* MIT License
* https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
* https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
*/
export const isArray: typeof Array.isArray = Array.isArray
export const isFunction = (val: unknown): val is Function => typeof val === 'function'
export const isString = (val: unknown): val is string => typeof val === 'string'
export const isBoolean = (val: unknown): val is boolean => typeof val === 'boolean'
export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isObject = (val: unknown): val is Record<any, any> =>
val !== null && typeof val === 'object'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
return isObject(val) && isFunction(val.then) && isFunction(val.catch)
}

export const objectToString: typeof Object.prototype.toString = Object.prototype.toString
export const toTypeString = (value: unknown): string => objectToString.call(value)

export const isPlainObject = (val: unknown): val is object =>
toTypeString(val) === '[object Object]'

// for converting list and named values to displayed strings.
export const toDisplayString = (val: unknown): string => {
return val == null
? ''
: isArray(val) || (isPlainObject(val) && val.toString === objectToString)
? JSON.stringify(val, null, 2)
: String(val)
}

export function join(items: string[], separator = ''): string {
return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '')
}

const RANGE = 2

export function generateCodeFrame(
source: string,
start: number = 0,
end: number = source.length
): string {
const lines = source.split(/\r?\n/)
let count = 0
const res: string[] = []
for (let i = 0; i < lines.length; i++) {
count += lines[i].length + 1
if (count >= start) {
for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
if (j < 0 || j >= lines.length) continue
const line = j + 1
res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`)
const lineLength = lines[j].length
if (j === i) {
// push underline
const pad = start - (count - lineLength) + 1
const length = Math.max(1, end > count ? lineLength - pad : end - start)
res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length))
} else if (j > i) {
if (end > count) {
const length = Math.max(Math.min(end - count, lineLength), 1)
res.push(` | ` + '^'.repeat(length))
}
count += lineLength + 1
}
}
break
}
}
return res.join('\n')
}
Loading
Loading