Skip to content

Commit

Permalink
[packages/utils] Linterの警告箇所を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
nemuvski committed Jan 31, 2024
1 parent 7fba55d commit 08ca1e0
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 42 deletions.
3 changes: 1 addition & 2 deletions packages/utils/src/functions/buildPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ export function buildPath(...paths: Array<string | number | null | undefined>) {
if (isString(p)) {
if (i === 0) {
return p.trim().replace(/\/*$/g, '')
} else {
return p.trim().replace(/(^\/*|\/*$)/g, '')
}
return p.trim().replace(/(^\/*|\/*$)/g, '')
}
// フォールバック
return p
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/src/functions/cls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNullish } from './isNullish'
import { isBoolean } from './isBoolean'
import { isString } from './isString'
import { isNullish } from './isNullish'
import { isNumber } from './isNumber'
import { isString } from './isString'

/**
* cls()の引数optionsの型
Expand Down Expand Up @@ -58,10 +58,10 @@ export function cls(
if (isString(value) || isNumber(value)) return options?.trimPeriod ? String(value).replace(/\.+/g, '') : String(value)
let res = ''
if (Array.isArray(value)) {
value.forEach((n) => {
for (const n of value) {
const childRes = cls(n, options)
if (childRes) res += (res && ' ') + childRes
})
}
} else {
for (const key in value) {
if (value[key]) res += (res && ' ') + key
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/functions/containParamInUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isString } from './isString'
import { escapeRegExpChars } from './escapeRegExpChars'
import { getQueryString } from './getQueryString'
import { isString } from './isString'
import { isURL } from './isURL'
import { escapeRegExpChars } from './escapeRegExpChars'

/**
* 引数urlで指定したパラメータ名が存在する場合はTrueを返却
Expand All @@ -25,7 +25,7 @@ import { escapeRegExpChars } from './escapeRegExpChars'
*/
export function containParamInUrl<P extends string>(url: string | URL | URLSearchParams, paramName: P) {
if (isString(url)) {
return new RegExp('(\\?|&)' + escapeRegExpChars(paramName) + '=', 'g').test(getQueryString(url))
return new RegExp(`(\\?|&)${escapeRegExpChars(paramName)}=`, 'g').test(getQueryString(url))
}
return isURL(url) ? url.searchParams.has(paramName) : url.has(paramName)
}
33 changes: 17 additions & 16 deletions packages/utils/src/functions/diffArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,25 @@ export function diffArray<T>(

if (!left.length) {
return { leftOnlyItems, rightOnlyItems: right, bothItems }
} else if (!right.length) {
}
if (!right.length) {
return { leftOnlyItems: left, rightOnlyItems, bothItems }
} else {
// 配列leftを起点に配列leftにしか無い要素、両方に存在する要素を評価する
for (const leftItem of left) {
const matchedIndex = right.findIndex((rightItem) => matchFn(leftItem, rightItem))
if (matchedIndex < 0) {
leftOnlyItems.push(leftItem)
} else {
bothItems.push(leftItem)
}
}

// 配列leftを起点に配列leftにしか無い要素、両方に存在する要素を評価する
for (const leftItem of left) {
const matchedIndex = right.findIndex((rightItem) => matchFn(leftItem, rightItem))
if (matchedIndex < 0) {
leftOnlyItems.push(leftItem)
} else {
bothItems.push(leftItem)
}
// 次に、配列rightから両方に存在する要素を除いたものを求める
for (const rightItem of right) {
const matchedIndex = bothItems.findIndex((bothItem) => matchFn(rightItem, bothItem))
if (matchedIndex < 0) {
rightOnlyItems.push(rightItem)
}
}
// 次に、配列rightから両方に存在する要素を除いたものを求める
for (const rightItem of right) {
const matchedIndex = bothItems.findIndex((bothItem) => matchFn(rightItem, bothItem))
if (matchedIndex < 0) {
rightOnlyItems.push(rightItem)
}
}
return { leftOnlyItems, rightOnlyItems, bothItems }
Expand Down
14 changes: 7 additions & 7 deletions packages/utils/src/functions/getQueryParamsValue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isString } from './isString'
import { escapeRegExpChars } from './escapeRegExpChars'
import { getQueryString } from './getQueryString'
import { isString } from './isString'
import { isURL } from './isURL'
import { escapeRegExpChars } from './escapeRegExpChars'

/**
* 引数urlから指定したパラメータをキーとした配列を値に持つオブジェクトを返却
Expand Down Expand Up @@ -33,14 +33,14 @@ export function getQueryParamsValue<P extends string>(
return res
}

paramNames.forEach((f) => {
for (const f of paramNames) {
res[f] = []
})
}

if (isString(url)) {
const queryStr = getQueryString(url)
const paramNamesPattern = paramNames.map((k) => escapeRegExpChars(k)).join('|')
const matches = queryStr.matchAll(new RegExp('(\\?|&)(' + paramNamesPattern + ')=([^&]*)', 'g'))
const matches = queryStr.matchAll(new RegExp(`(\\?|&)(${paramNamesPattern})=([^&]*)`, 'g'))
for (const m of matches) {
// NOTE: 添字2はパラメータ名グループ, 添字3はパラメータの値グループ
if (m[2] !== undefined && m[3] !== undefined) {
Expand All @@ -49,9 +49,9 @@ export function getQueryParamsValue<P extends string>(
}
} else {
const searchParams = isURL(url) ? url.searchParams : url
paramNames.forEach((param) => {
for (const param of paramNames) {
res[param] = searchParams.getAll(param)
})
}
}

return res
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/functions/removeControlChars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export function removeControlChars(str: string, options?: RemoveControlCharsOpti
if (options.lf) regexStr += '\x0A'
if (options.cr) regexStr += '\x0D'
}
return str.replace(new RegExp('[' + regexStr + ']', 'g'), '')
return str.replace(new RegExp(`[${regexStr}]`, 'g'), '')
}
2 changes: 1 addition & 1 deletion packages/utils/src/functions/removeZWChars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ export function removeZWChars(str: string, options?: RemoveZWCharsOptions) {
if (options.rlm) regexStr += '\u200f'
if (options.zwnbsp) regexStr += '\ufeff'
}
return str.replace(new RegExp('[' + regexStr + ']', 'g'), '')
return str.replace(new RegExp(`[${regexStr}]`, 'g'), '')
}
4 changes: 2 additions & 2 deletions packages/utils/src/functions/separateArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
export function separateArray<T, M extends T, N extends T>(arr: Array<T>, matchFn: (v: T) => boolean) {
const matches: Array<M> = []
const notMatches: Array<N> = []
arr.forEach((v) => {
for (const v of arr) {
if (matchFn(v)) {
// @ts-ignore: MはTと異なる場合がある
matches.push(v)
} else {
// @ts-ignore: NはTと異なる場合がある
notMatches.push(v)
}
})
}
return { matches, notMatches }
}
2 changes: 1 addition & 1 deletion packages/utils/src/functions/strf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { escapeRegExpChars } from './escapeRegExpChars'
export function strf<F extends Record<string, Primitive>>(
str: string,
fields: F,
transFn: (fieldName: keyof F, fieldValue: F[keyof F]) => string = (n, v) => String(v).toString()
transFn: (fieldName: keyof F, fieldValue: F[keyof F]) => string = (_n, v) => String(v).toString()
) {
if (!Object.keys(fields).length) {
return str
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/functions/withRootRelativePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
* withRootRelativePath('path/to/test')
*/
export function withRootRelativePath(path: string) {
return '/' + path.trim().replace(/^\/+/, '')
return `/${path.trim().replace(/^\/+/, '')}`
}
2 changes: 1 addition & 1 deletion packages/utils/src/functions/withTrailingSlash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import { removeTrailingSlash } from './removeTrailingSlash'
* withTrailingSlash('http://localhost:8000/sample')
*/
export function withTrailingSlash(path: string) {
return removeTrailingSlash(path) + '/'
return `${removeTrailingSlash(path)}/`
}
2 changes: 1 addition & 1 deletion packages/utils/tests/functions/separateArray.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from 'vitest'
import { isNotNullish, isNumber, Nullish, separateArray } from '../../src'
import { isNotNullish, isNumber, type Nullish, separateArray } from '../../src'

describe('separateArray()', () => {
test('separateArray() - Pattern1', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/tests/types/aliases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectTypeOf, describe, test } from 'vitest'
import { Primitive, Falsy, Nullish } from '../../src'
import type { Primitive, Falsy, Nullish } from '../../src'

describe('types/aliases.ts', () => {
const date = new Date('August 19, 1975 23:15:30')
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/tests/types/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectTypeOf, describe, test } from 'vitest'
import {
import type {
MatchTypeKeys,
ExactMatchTypeKeys,
NotMatchTypeKeys,
Expand Down

0 comments on commit 08ca1e0

Please sign in to comment.