Skip to content

Commit

Permalink
chore: fix warnings on lgtm.com
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed May 14, 2020
1 parent 9218991 commit f00e288
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 93 deletions.
8 changes: 4 additions & 4 deletions packages/x6/src/common/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ export class Events<EventArgs extends Events.EventArgs = any> {
off(): this
off(name: null, handler: Events.Handler<any>): this
off(name: null, handler: null, context: any): this
off<Name extends Events.EventNames<EventArgs>>(name: Name): this
off<Name extends Events.EventNames<EventArgs>>(
name: Name,
handler: Events.Handler<EventArgs[Name]>,
context: any,
): this
off<Name extends Events.EventNames<EventArgs>>(
name: Name,
handler: Events.Handler<EventArgs[Name]>,
context: any,
): this
off<Name extends Events.UnknownNames<EventArgs>>(name: Name): this
off<Name extends Events.EventNames<EventArgs>>(name: Name): this
off<Name extends Events.UnknownNames<EventArgs>>(
name: Name,
handler: Events.Handler<any>,
context: any,
): this
off<Name extends Events.UnknownNames<EventArgs>>(
name: Name,
handler: Events.Handler<any>,
context: any,
): this
off<Name extends Events.UnknownNames<EventArgs>>(name: Name): this
off(
name?: string | null,
handler?: Events.Handler<any> | null,
Expand Down
98 changes: 9 additions & 89 deletions packages/x6/src/util/object/object.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
export {
has,
pick,
forIn,
merge,
extend,
isEqual,
isEmpty,
isObject,
isPlainObject,
isEqual,
clone,
cloneDeep,
merge,
pick,
defaults,
defaultsDeep,
forIn,
extend,
} from 'lodash-es'

export * from './mixins'
export * from './inherit'

import { has, isPlainObject } from 'lodash-es'

export function ensure<T>(value: T | null | undefined, defaultValue: T) {
return value != null ? value : defaultValue!
}
Expand Down Expand Up @@ -82,7 +81,7 @@ export function setByPath(
const lastKey = keys.pop()
if (lastKey) {
let diver = obj
keys.forEach(key => {
keys.forEach((key) => {
if (diver[key] == null) {
diver[key] = {}
}
Expand Down Expand Up @@ -121,7 +120,7 @@ export function flatten(
) {
const ret: { [key: string]: any } = {}

Object.keys(obj).forEach(key => {
Object.keys(obj).forEach((key) => {
const val = obj[key]
let deep = typeof val === 'object' || Array.isArray(val)
if (deep && stop && stop(val)) {
Expand All @@ -130,7 +129,7 @@ export function flatten(

if (deep) {
const flatObject = flatten(val, delim, stop)
Object.keys(flatObject).forEach(flatKey => {
Object.keys(flatObject).forEach((flatKey) => {
ret[key + delim + flatKey] = flatObject[flatKey]
})
} else {
Expand All @@ -144,82 +143,3 @@ export function flatten(

return ret
}

export function mergec(
target: { [key: string]: any },
source: { [key: string]: any },
options: {
ignoreNull?: boolean
ignoreUndefined?: boolean
decorator?: (
source: { [key: string]: any },
target: { [key: string]: any },
key: string,
) => any
} = {},
) {
for (const name in source) {
const src = target[name]
const copy = source[name]
const copyIsArray = Array.isArray(copy)

if (copyIsArray || isPlainObject(copy)) {
let clone

if (copyIsArray) {
clone = src && Array.isArray(src) ? src : []
} else {
clone = src && isPlainObject(src) ? src : {}
}

target[name] = mergec(clone, copy, options)
} else {
if (
copy != null ||
(copy === null && options.ignoreNull !== true) ||
(copy === undefined && options.ignoreUndefined !== true)
) {
target[name] = options.decorator
? options.decorator(target, source, name)
: copy
}
}
}

return target
}

export function destroy(obj: any) {
if (obj != null) {
for (const prop in obj) {
if (has(obj, prop)) {
delete obj[prop]
}
}
obj.prototype = obj.__proto__ = null
obj.destroyed = true
}
}

export function clone<T>(
obj: any,
ignores?: string[],
shallow: boolean = false,
) {
if (obj != null && typeof obj.constructor === 'function') {
const result = new obj.constructor() as { [key: string]: any }
Object.keys(obj).forEach(key => {
if (ignores == null || !ignores.includes(key)) {
if (!shallow && typeof obj[key] === 'object') {
result[key] = clone(obj[key])
} else {
result[key] = obj[key]
}
}
})

return result as T
}

return null
}

0 comments on commit f00e288

Please sign in to comment.