Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove isString, isNumber #500

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
"rules": {
"react/jsx-key": "off"
}
},
{
"files": ["**/*.{js,jsx,ts,tsx}"],
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "CallExpression[callee.name='isNumber']",
"message": "use native typeof === 'number' check"
},
{
"selector": "CallExpression[callee.name='isString']",
"message": "use native typeof === 'string' check"
}
]
}
}
],
"parser": "@typescript-eslint/parser",
Expand Down
5 changes: 3 additions & 2 deletions src/handler/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import cssColorParse from 'parse-css-color'

import CssDimension from '../vendor/parse-css-dimension/index.js'
import parseTransformOrigin from '../transform-origin.js'
import { isString, lengthToNumber, v } from '../utils.js'
import { lengthToNumber, v } from '../utils.js'

// https://react-cn.github.io/react/tips/style-props-value-px.html
const optOutPx = new Set([
Expand Down Expand Up @@ -221,6 +221,7 @@ function getErrorHint(name: string) {
}

const RGB_SLASH = /rgb\((\d+)\s+(\d+)\s+(\d+)\s*\/\s*([\.\d]+)\)/

function normalizeColor(value: string | object) {
if (typeof value === 'string') {
if (RGB_SLASH.test(value.trim())) {
Expand Down Expand Up @@ -429,7 +430,7 @@ function preprocess(
value: string | number,
currentColor: string
): string | number {
if (isString(value)) {
if (typeof value === 'string') {
value = convertCurrentColorToActualValue(value, currentColor)
}

Expand Down
11 changes: 5 additions & 6 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import {
buildXMLString,
splitByBreakOpportunities,
isUndefined,
isString,
lengthToNumber,
isNumber,
} from './utils.js'
import buildText, { container } from './builder/text.js'
import { buildDropShadow } from './builder/shadow.js'
Expand Down Expand Up @@ -146,9 +144,10 @@ export default async function* buildTextNodes(
return measureGraphemeArray(segment(text, 'grapheme'))
}

const tabWidth = isString(tabSize)
? lengthToNumber(tabSize, fontSize as number, 1, parentStyle)
: measureGrapheme(Space) * (tabSize as number)
const tabWidth =
typeof tabSize === 'string'
? lengthToNumber(tabSize, fontSize as number, 1, parentStyle)
: measureGrapheme(Space) * (tabSize as number)

const calc = (
text: string,
Expand Down Expand Up @@ -846,7 +845,7 @@ function processTextOverflow(
textOverflow === 'ellipsis' &&
display === '-webkit-box' &&
WebkitBoxOrient === 'vertical' &&
isNumber(WebkitLineClamp) &&
typeof WebkitLineClamp === 'number' &&
WebkitLineClamp > 0
) {
return [WebkitLineClamp, HorizontalEllipsis]
Expand Down
8 changes: 0 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,6 @@ export function toString(x: unknown): string {
return Object.prototype.toString.call(x)
}

export function isString(x: unknown): x is string {
return typeof x === 'string'
}

export function isNumber(x: unknown): x is number {
return typeof x === 'number'
}

export function isUndefined(x: unknown): x is undefined {
return toString(x) === '[object Undefined]'
}
Expand Down