mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor: use native hasOwn (#1177)
This commit is contained in:
parent
a4a5a8b652
commit
5537732ba3
@ -1,30 +0,0 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
/**
|
||||
* Determines whether an object has a property with the specified name.
|
||||
* Avoid calling prototype builtin `hasOwnProperty` for two reasons:
|
||||
*
|
||||
* 1. `hasOwnProperty` is defined on the object as something else:
|
||||
*
|
||||
* const options = {
|
||||
* ending: 'utf8',
|
||||
* hasOwnProperty: 'foo'
|
||||
* };
|
||||
* options.hasOwnProperty('ending') // throws a TypeError
|
||||
*
|
||||
* 2. The object doesn't inherit from `Object.prototype`:
|
||||
*
|
||||
* const options = Object.create(null);
|
||||
* options.ending = 'utf8';
|
||||
* options.hasOwnProperty('ending'); // throws a TypeError
|
||||
*
|
||||
* @param obj A Object.
|
||||
* @param v A property name.
|
||||
* @see https://eslint.org/docs/rules/no-prototype-builtins
|
||||
*/
|
||||
export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return Object.prototype.hasOwnProperty.call(obj, v);
|
||||
}
|
@ -12,7 +12,7 @@ type Any = common.Any;
|
||||
type ArrayObject<T = Any> = common.ArrayObject<T>;
|
||||
|
||||
const _toString = Object.prototype.toString;
|
||||
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
const { hasOwn } = Object;
|
||||
|
||||
const CHAR_TAB = 0x09; /* Tab */
|
||||
const CHAR_LINE_FEED = 0x0a; /* LF */
|
||||
@ -716,7 +716,7 @@ function detectType(
|
||||
|
||||
if (_toString.call(type.represent) === "[object Function]") {
|
||||
_result = (type.represent as RepresentFn)(object, style);
|
||||
} else if (_hasOwnProperty.call(type.represent, style)) {
|
||||
} else if (hasOwn(type.represent, style)) {
|
||||
_result = (type.represent as ArrayObject<RepresentFn>)[style](
|
||||
object,
|
||||
style,
|
||||
|
@ -8,7 +8,7 @@ import { State } from "../state.ts";
|
||||
import type { StyleVariant, Type } from "../type.ts";
|
||||
import type { Any, ArrayObject } from "../utils.ts";
|
||||
|
||||
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
const { hasOwn } = Object;
|
||||
|
||||
function compileStyleMap(
|
||||
schema: Schema,
|
||||
@ -31,7 +31,7 @@ function compileStyleMap(
|
||||
if (
|
||||
type &&
|
||||
typeof type.styleAliases !== "undefined" &&
|
||||
_hasOwnProperty.call(type.styleAliases, style)
|
||||
hasOwn(type.styleAliases, style)
|
||||
) {
|
||||
style = type.styleAliases[style];
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import { LoaderState, LoaderStateOptions, ResultType } from "./loader_state.ts";
|
||||
type Any = common.Any;
|
||||
type ArrayObject<T = Any> = common.ArrayObject<T>;
|
||||
|
||||
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
const { hasOwn } = Object;
|
||||
|
||||
const CONTEXT_FLOW_IN = 1;
|
||||
const CONTEXT_FLOW_OUT = 2;
|
||||
@ -231,7 +231,7 @@ const directiveHandlers: DirectiveHandlers = {
|
||||
);
|
||||
}
|
||||
|
||||
if (_hasOwnProperty.call(state.tagMap, handle)) {
|
||||
if (state.tagMap && hasOwn(state.tagMap, handle)) {
|
||||
return throwError(
|
||||
state,
|
||||
`there is a previously declared suffix for "${handle}" tag handle`,
|
||||
@ -299,7 +299,7 @@ function mergeMappings(
|
||||
const keys = Object.keys(source);
|
||||
for (let i = 0, len = keys.length; i < len; i++) {
|
||||
const key = keys[i];
|
||||
if (!_hasOwnProperty.call(destination, key)) {
|
||||
if (!hasOwn(destination, key)) {
|
||||
destination[key] = (source as ArrayObject)[key];
|
||||
overridableKeys[key] = true;
|
||||
}
|
||||
@ -364,8 +364,8 @@ function storeMappingPair(
|
||||
} else {
|
||||
if (
|
||||
!state.json &&
|
||||
!_hasOwnProperty.call(overridableKeys, keyNode) &&
|
||||
_hasOwnProperty.call(result, keyNode)
|
||||
!hasOwn(overridableKeys, keyNode) &&
|
||||
hasOwn(result, keyNode)
|
||||
) {
|
||||
state.line = startLine || state.line;
|
||||
state.position = startPos || state.position;
|
||||
@ -1349,7 +1349,7 @@ function readTagProperty(state: LoaderState): boolean {
|
||||
state.tag = tagName;
|
||||
} else if (
|
||||
typeof state.tagMap !== "undefined" &&
|
||||
_hasOwnProperty.call(state.tagMap, tagHandle)
|
||||
hasOwn(state.tagMap, tagHandle)
|
||||
) {
|
||||
state.tag = state.tagMap[tagHandle] + tagName;
|
||||
} else if (tagHandle === "!") {
|
||||
@ -1410,7 +1410,7 @@ function readAlias(state: LoaderState): boolean {
|
||||
const alias = state.input.slice(_position, state.position);
|
||||
if (
|
||||
typeof state.anchorMap !== "undefined" &&
|
||||
!Object.prototype.hasOwnProperty.call(state.anchorMap, alias)
|
||||
!hasOwn(state.anchorMap, alias)
|
||||
) {
|
||||
return throwError(state, `unidentified alias "${alias}"`);
|
||||
}
|
||||
@ -1565,7 +1565,7 @@ function composeNode(
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
_hasOwnProperty.call(state.typeMap[state.kind || "fallback"], state.tag)
|
||||
hasOwn(state.typeMap[state.kind || "fallback"], state.tag)
|
||||
) {
|
||||
type = state.typeMap[state.kind || "fallback"][state.tag];
|
||||
|
||||
@ -1664,7 +1664,7 @@ function readDocument(state: LoaderState): void {
|
||||
|
||||
if (ch !== 0) readLineBreak(state);
|
||||
|
||||
if (_hasOwnProperty.call(directiveHandlers, directiveName)) {
|
||||
if (hasOwn(directiveHandlers, directiveName)) {
|
||||
directiveHandlers[directiveName](state, directiveName, ...directiveArgs);
|
||||
} else {
|
||||
throwWarning(state, `unknown document directive "${directiveName}"`);
|
||||
|
@ -6,7 +6,7 @@
|
||||
import { Type } from "../type.ts";
|
||||
import type { Any } from "../utils.ts";
|
||||
|
||||
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
const { hasOwn } = Object;
|
||||
const _toString = Object.prototype.toString;
|
||||
|
||||
function resolveYamlOmap(data: Any): boolean {
|
||||
@ -20,7 +20,7 @@ function resolveYamlOmap(data: Any): boolean {
|
||||
if (_toString.call(pair) !== "[object Object]") return false;
|
||||
|
||||
for (pairKey in pair) {
|
||||
if (_hasOwnProperty.call(pair, pairKey)) {
|
||||
if (hasOwn(pair, pairKey)) {
|
||||
if (!pairHasKey) pairHasKey = true;
|
||||
else return false;
|
||||
}
|
||||
|
@ -6,13 +6,13 @@
|
||||
import { Type } from "../type.ts";
|
||||
import type { Any } from "../utils.ts";
|
||||
|
||||
const _hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
const { hasOwn } = Object;
|
||||
|
||||
function resolveYamlSet(data: Any): boolean {
|
||||
if (data === null) return true;
|
||||
|
||||
for (const key in data) {
|
||||
if (_hasOwnProperty.call(data, key)) {
|
||||
if (hasOwn(data, key)) {
|
||||
if (data[key] !== null) return false;
|
||||
}
|
||||
}
|
||||
|
@ -61,8 +61,10 @@ interface NestedMapping {
|
||||
[key: string]: NestedMapping | unknown;
|
||||
}
|
||||
|
||||
const { hasOwn } = Object;
|
||||
|
||||
function get<T>(obj: Record<string, T>, key: string): T | undefined {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
if (hasOwn(obj, key)) {
|
||||
return obj[key];
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ import { extname } from "../path/mod.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { assert } from "../_util/assert.ts";
|
||||
import { TextProtoReader } from "../textproto/mod.ts";
|
||||
import { hasOwnProperty } from "../_util/has_own_property.ts";
|
||||
import { Buffer } from "../io/buffer.ts";
|
||||
import { copy } from "../io/util.ts";
|
||||
|
||||
const { hasOwn } = Object;
|
||||
/** FormFile object */
|
||||
export interface FormFile {
|
||||
/** filename */
|
||||
@ -29,7 +29,7 @@ export interface FormFile {
|
||||
/** Type guard for FormFile */
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function isFormFile(x: any): x is FormFile {
|
||||
return hasOwnProperty(x, "filename") && hasOwnProperty(x, "type");
|
||||
return hasOwn(x, "filename") && hasOwn(x, "type");
|
||||
}
|
||||
|
||||
function randomBoundary(): string {
|
||||
|
@ -47,6 +47,7 @@ import { assert } from "../_util/assert.ts";
|
||||
import { fileURLToPath, pathToFileURL } from "./url.ts";
|
||||
import { isWindows } from "../_util/os.ts";
|
||||
|
||||
const { hasOwn } = Object;
|
||||
const CHAR_FORWARD_SLASH = "/".charCodeAt(0);
|
||||
const CHAR_BACKWARD_SLASH = "\\".charCodeAt(0);
|
||||
const CHAR_COLON = ":".charCodeAt(0);
|
||||
@ -898,7 +899,7 @@ function applyExports(basePath: string, expansion: string): string {
|
||||
}
|
||||
|
||||
if (typeof pkgExports === "object") {
|
||||
if (Object.prototype.hasOwnProperty.call(pkgExports, mappingKey)) {
|
||||
if (hasOwn(pkgExports, mappingKey)) {
|
||||
const mapping = pkgExports[mappingKey];
|
||||
return resolveExportsTarget(
|
||||
pathToFileURL(basePath + "/"),
|
||||
@ -1030,7 +1031,7 @@ function resolveExportsTarget(
|
||||
if (key !== "default" && !cjsConditions.has(key)) {
|
||||
continue;
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(target, key)) {
|
||||
if (hasOwn(target, key)) {
|
||||
try {
|
||||
return resolveExportsTarget(
|
||||
pkgPath,
|
||||
@ -1089,7 +1090,7 @@ const CircularRequirePrototypeWarningProxy = new Proxy(
|
||||
},
|
||||
|
||||
getOwnPropertyDescriptor(target, prop): PropertyDescriptor | undefined {
|
||||
if (Object.prototype.hasOwnProperty.call(target, prop)) {
|
||||
if (hasOwn(target, prop)) {
|
||||
return Object.getOwnPropertyDescriptor(target, prop);
|
||||
}
|
||||
emitCircularRequireWarning(prop);
|
||||
|
@ -1,5 +1,4 @@
|
||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
import { hasOwnProperty } from "../_util/has_own_property.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
|
||||
import { crypto } from "../crypto/mod.ts";
|
||||
@ -11,6 +10,7 @@ import { Deferred, deferred } from "../async/deferred.ts";
|
||||
import { assert } from "../_util/assert.ts";
|
||||
import { concat } from "../bytes/mod.ts";
|
||||
|
||||
const { hasOwn } = Object;
|
||||
export enum OpCode {
|
||||
Continue = 0x0,
|
||||
TextFrame = 0x1,
|
||||
@ -36,7 +36,8 @@ export interface WebSocketCloseEvent {
|
||||
export function isWebSocketCloseEvent(
|
||||
a: WebSocketEvent,
|
||||
): a is WebSocketCloseEvent {
|
||||
return hasOwnProperty(a, "code");
|
||||
// deno-lint-ignore ban-types
|
||||
return hasOwn(a as object, "code");
|
||||
}
|
||||
|
||||
export type WebSocketPingEvent = ["ping", Uint8Array];
|
||||
|
Loading…
Reference in New Issue
Block a user