1 Star 0 Fork 15

苏杰豪/Vimium C

forked from gdh1995/Vimium C 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
polyfill.ts 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
gdh1995 提交于 2020-08-05 23:48 +08:00 . noop: update some text in UI / code
/// <reference no-default-lib="true"/>
/// <reference lib="es6" />
(function (): void {
type primitiveObject = boolean | number | string;
type primitive = primitiveObject | null | undefined;
type ObjectCoercible = primitiveObject | {
toString (): primitive;
} | {
valueOf (): primitive;
};
type anyNotSymbol = ObjectCoercible | null | undefined;
interface StandardString {
endsWith? (this: string, searchString: string, pos?: number | undefined): boolean;
endsWith? (this: ObjectCoercible, searchString?: anyNotSymbol, pos?: anyNotSymbol): boolean;
includes? (this: string, searchString: string, pos?: number | undefined): boolean;
includes? (this: ObjectCoercible, searchString?: anyNotSymbol, pos?: anyNotSymbol): boolean;
startsWith? (this: string, searchString: string, pos?: number | undefined): boolean;
startsWith? (this: ObjectCoercible, searchString?: anyNotSymbol, pos?: anyNotSymbol): boolean;
}
const symMatch = typeof Symbol === "function" && typeof Symbol.match === "symbol" &&
(Symbol.match as symbol | string as "Symbol(Symbol.match)"),
// eslint-disable-next-line id-denylist
StrCls = String as StringConstructor & { readonly prototype: StandardString }, TECls = TypeError,
StrProto = StrCls.prototype,
toStr = Object.prototype.toString;
"".startsWith || (
StrProto.startsWith = function startsWith(this: ObjectCoercible, searchString: anyNotSymbol): boolean {
const err = check(this, searchString), a = this != null && err !== 1 ? StrCls(this) : "";
if (err !== 0) {
if (err === 1 || err === 2) { return !((err < 2 ? this : searchString) + ""); }
throw new TECls(err.replace("${func}", "startsWith"));
}
let b = StrCls(searchString), args = arguments, c = args.length > 1 ? +args[1] : 0;
c = c > 0 ? c | 0 : 0;
c > a.length && (c = a.length);
return a.lastIndexOf(b, c) === c;
});
"".endsWith || (
StrProto.endsWith = function endsWith(this: ObjectCoercible, searchString: anyNotSymbol): boolean {
const err = check(this, searchString), a = this != null && err !== 1 ? StrCls(this) : "";
if (err !== 0) {
if (err === 1 || err === 2) { return !((err < 2 ? this : searchString) + ""); }
throw new TECls(err.replace("${func}", "endsWith"));
}
let b = StrCls(searchString), args = arguments, u: undefined, c: number
, p: primitive | object = args.length > 1 ? args[1] : u, l = a.length;
c = (p === u ? l : (c = +<number | string> p) > 0 ? c | 0 : 0) - b.length;
c > l && (c = l);
return c >= 0 && a.indexOf(b, c) === c;
});
"".includes || (
StrProto.includes = function includes(this: ObjectCoercible, searchString: anyNotSymbol): boolean {
const err = check(this, searchString), a = this != null && err !== 1 ? StrCls(this) : "";
if (err !== 0) {
if (err === 1 || err === 2) { return !((err < 2 ? this : searchString) + ""); }
throw new TECls(err.replace("${func}", "includes"));
}
let b = StrCls(searchString), args = arguments, c = args.length > 1 ? +args[1] : 0;
c = c > 0 ? c | 0 : 0;
c > a.length && (c = a.length);
// eslint-disable-next-line @typescript-eslint/prefer-includes
return a.indexOf(b, c) >= 0;
});
function check(a: primitive | object, b: primitive | object): 0 | string | 1 | 2 {
/** note: should never call `valueOf` or `toString` on a / b */
if (a == null) { return "String.prototype.${func} called on null or undefined"; }
if (!b) { return 0; }
let t: 0 | 1 | 2 = typeof a === "symbol" ? 1 : typeof b === "symbol" ? 2 : 0;
if (t) { return t; }
interface PossibleTypeOfB {
[key: string]: ((this: string, re: RegExp) => boolean) | primitive;
}
let f: PossibleTypeOfB[string], u: undefined
, i = symMatch && (f = (b as PossibleTypeOfB)[symMatch]) !== u ? f
: toStr.call(b) === "[object RegExp]";
return i ? "First argument to String.prototype.${func} must not be a regular expression" : 0;
}
})();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/Megasu/vimium-c.git
git@gitee.com:Megasu/vimium-c.git
Megasu
vimium-c
Vimium C
master

搜索帮助