Skip to content

Commit 77139bd

Browse files
committed
feat: place common code in shared
1 parent daf6eda commit 77139bd

File tree

7 files changed

+25
-31
lines changed

7 files changed

+25
-31
lines changed

packages/ai/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GET_ELEMENT_SELECTORS_CONTEXT,
66
HELP_CONTEXT
77
} from './context'
8+
import { isObject } from '../shared'
89

910
type OpenAIChatModel =
1011
| 'gpt-4-0125-preview'
@@ -127,7 +128,7 @@ export function createXCrawlOpenAI(
127128
const { model } = option
128129

129130
let coderContent: string = ''
130-
if (typeof content === 'object' && content !== null) {
131+
if (isObject(content)) {
131132
coderContent = JSON.stringify(content)
132133
} else {
133134
const obj: XCrawlOpenAIParseElementsContentOptions = {
@@ -155,7 +156,7 @@ export function createXCrawlOpenAI(
155156
const { model } = option
156157

157158
let coderContent: string = ''
158-
if (typeof content === 'object' && content !== null) {
159+
if (isObject(content)) {
159160
coderContent = JSON.stringify(content)
160161
} else {
161162
const obj: XCrawlOpenAIGetElementSelectorsContentOptions = {

packages/crawl/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
whiteBold,
2222
isPromise,
2323
isBuffer
24-
} from './utils'
24+
} from '../shared'
2525

2626
import {
2727
CrawlDataDetailTargetConfig,

packages/crawl/batchCrawl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
random,
77
sleep,
88
whiteBold
9-
} from './utils'
9+
} from '../shared'
1010

1111
import type { InfoCommonConfig } from './api'
1212
import { CrawlDetail, Device } from './controller'

packages/crawl/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
logSuccess,
2121
logWarn,
2222
whiteBold
23-
} from './utils'
23+
} from '../shared'
2424
import { HTTPResponse } from 'puppeteer'
2525
import { Request } from './request'
2626
import { CrawlCommonResult } from './types/api'

packages/crawl/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from './api'
77

88
import { CreateCrawlConfig, CrawlApp, CrawlBaseConfig } from './types'
9-
import { isBoolean, isObject } from './utils'
9+
import { isBoolean, isObject } from '../shared'
1010

1111
let id = 0
1212

packages/crawl/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import querystring from 'node:querystring'
1010

1111
import { HttpsProxyAgent } from 'https-proxy-agent'
1212

13-
import { isObject, isUndefined } from './utils'
13+
import { isObject, isUndefined } from '../shared'
1414

1515
import { AnyObject } from './types/common'
1616
import { LoaderCrawlDataDetail, LoaderCrawlFileDetail } from './api'

packages/crawl/utils.ts renamed to packages/shared/index.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,30 @@ export const logWarn = chalk.yellow
1111
export const logNumber = chalk.hex('#a57fff')
1212
export const whiteBold = chalk.white.bold
1313

14-
export function isUndefined(value: any): value is undefined {
15-
return typeof value === 'undefined'
16-
}
14+
export const isUndefined = (value: unknown): value is undefined =>
15+
typeof value === 'undefined'
1716

18-
export function isNumber(value: any): value is number {
19-
return typeof value === 'number'
20-
}
17+
export const isNumber = (value: unknown): value is number =>
18+
typeof value === 'number'
2119

22-
export function isString(value: any): value is string {
23-
return typeof value === 'string'
24-
}
20+
export const isString = (value: unknown): value is string =>
21+
typeof value === 'string'
2522

26-
export function isBoolean(value: any): value is string {
27-
return typeof value === 'boolean'
28-
}
23+
export const isBoolean = (value: unknown): value is string =>
24+
typeof value === 'boolean'
2925

30-
export function isObject(value: any): value is object {
31-
return typeof value === 'object' && value !== null && !Array.isArray(value)
32-
}
26+
export const isObject = (value: unknown): value is Record<any, any> =>
27+
typeof value === 'object' && value !== null && !Array.isArray(value)
3328

34-
export function isArray(value: any): value is any[] {
35-
return Array.isArray(value)
36-
}
29+
export const isArray = Array.isArray
3730

38-
export function isPromise(value: any): value is Promise<any> {
39-
return typeof value === 'function' && !isUndefined(value.then)
40-
}
31+
export const isFunction = (value: unknown): value is Function =>
32+
typeof value === 'function'
4133

42-
export function isBuffer(value: any): value is Buffer {
43-
return Buffer.isBuffer(value)
44-
}
34+
export const isPromise = (value: any): value is Promise<any> =>
35+
isObject(value) && isFunction(value.then) && isFunction(value.catch)
36+
37+
export const isBuffer = Buffer.isBuffer
4538

4639
export function sleep(timeout: number) {
4740
return new Promise((resolve) => setTimeout(resolve, timeout))

0 commit comments

Comments
 (0)