Skip to content

Commit

Permalink
feat(utils): 新增 utils 方法
Browse files Browse the repository at this point in the history
新增upperFirst, random, getGlobalThis, isArrayBuffer 等方法
  • Loading branch information
vfiee committed Jun 2, 2022
1 parent 3af5f1e commit da9bf29
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 55 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"docwriter.style": "JSDoc"
"docwriter.style": "JSDoc",
"cSpell.words": [
"vhooks"
]
}
12 changes: 10 additions & 2 deletions packages/utils/__tests__/is/is.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
isFormData,
isStream,
isUrlSearchParams,
isPrimitive
isPrimitive,
isArrayBuffer
} from '../../src'

describe('is', () => {
Expand Down Expand Up @@ -310,7 +311,7 @@ describe('is', () => {
expect(isBlob(blob)).toBe(true)
})
test('isFile', () => {
const filePath = path.join(__dirname, '../../package.json')
const filePath = path.join(__dirname, 'is.spec.ts')
const file = new File(
[fs.readFileSync(filePath).toString()],
'package.json',
Expand All @@ -333,4 +334,11 @@ describe('is', () => {
expect(isStream(readStream)).toBe(true)
expect(isStream(writeStream)).toBe(true)
})

test('isArrayBuffer', () => {
const buffer = new ArrayBuffer(10)
const array = new Array(10)
expect(isArrayBuffer(buffer)).toBe(true)
expect(isArrayBuffer(array)).toBe(false)
})
})
13 changes: 13 additions & 0 deletions packages/utils/__tests__/number/number.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { random } from '../../src'

describe('number', () => {
test('should return random number between min and max', () => {
const min = 1
const max = 10
for (let i = 0; i < max; i++) {
const result = random(min, max)
expect(result).toBeGreaterThanOrEqual(min)
expect(result).toBeLessThanOrEqual(max)
}
})
})
18 changes: 16 additions & 2 deletions packages/utils/__tests__/object/object.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { get, isEmpty, set, unset } from '../../src'
import {
get,
isEmpty,
set,
unset,
getGlobalThis,
isObject,
isFunction
} from '../../src'

describe('utils/common', () => {
describe('object', () => {
test('get object', () => {
const obj: object = { a: 1, b: [1, 2, 3], c: { a: 1, b: 2, c: 3 } }
expect(get(obj, 'a')).toBe(1)
Expand Down Expand Up @@ -62,4 +70,10 @@ describe('utils/common', () => {
expect(isEmpty(arr)).toBe(false)
expect(arr[0]).toBeUndefined()
})
test('should return global context', () => {
const global = getGlobalThis()
expect(isObject(global)).toBe(true)
expect(isFunction(global.setTimeout)).toBe(true)
expect(isFunction(global.Object)).toBe(true)
})
})
9 changes: 9 additions & 0 deletions packages/utils/__tests__/string/string.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { upperFirst } from '../../src'

describe('string', () => {
test('should upperCase first letter', () => {
expect(upperFirst('hello world')).toBe('Hello world')
expect(upperFirst('HELLO WORLD')).toBe('HELLO WORLD')
expect(upperFirst(String({}))).toBe('[object Object]')
})
})
2 changes: 1 addition & 1 deletion packages/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (process.env.NODE_ENV === 'production') {
module.exports = require('./dist/utils.prod.cjs.js')
module.exports = require('./dist/utils.cjs.prod.js')
} else {
module.exports = require('./dist/utils.cjs.js')
}
6 changes: 5 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: vyron
* @Date: 2022-04-02 14:12:59
* @LastEditTime: 2022-05-24 14:16:30
* @LastEditTime: 2022-06-01 16:34:29
* @LastEditors: vyron
* @Description: 融合汇聚工具类导出
* @FilePath: /utils/packages/utils/src/index.ts
Expand All @@ -12,3 +12,7 @@ export * from './object'
export * from './array'

export * from './is'

export * from './number'

export * from './string'
11 changes: 10 additions & 1 deletion packages/utils/src/is/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: vyron
* @Date: 2022-01-10 17:38:09
* @LastEditTime: 2022-05-24 14:22:53
* @LastEditTime: 2022-06-02 14:30:44
* @LastEditors: vyron
* @Description: 判断数据类型
* @FilePath: /utils/packages/utils/src/is/index.ts
Expand Down Expand Up @@ -378,3 +378,12 @@ export function isArrayBufferView(value: unknown): value is ArrayBufferView {
const buffer = (value as any)?.buffer
return buffer && buffer instanceof ArrayBuffer
}

/**
* Returns whether the value is isArrayBuffer
* @param {unknown} value Any legal JavaScript value
* @returns {boolean} Returns true if value is isArrayBuffer, otherwise false
*/
export function isArrayBuffer(value: unknown): value is ArrayBuffer {
return toRawType(value) === 'ArrayBuffer'
}
25 changes: 25 additions & 0 deletions packages/utils/src/number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* @Author: vyron
* @Date: 2022-06-01 16:26:31
* @LastEditTime: 2022-06-01 17:08:12
* @LastEditors: vyron
* @Description: number utils
* @FilePath: /utils/packages/utils/src/number/index.ts
*/

/**
* Returns a random number between the minimum and maximum values.
* Configure floating to true to specify decimals and false by default
* @param {number} [min=0] - The minimum value
* @param {number} [max=1] - The maximum value
* @param {boolean} [floating=false] - If true, the number will be a float
* @returns {number} - The random number between the minimum and maximum values.
*/
export function random(
min: number = 0,
max: number = 1,
floating: boolean = false
): number {
const number = Math.random() * (max - min + 1) + min
return floating ? number : Math.floor(number)
}
27 changes: 27 additions & 0 deletions packages/utils/src/object/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* @Author: vyron
* @Date: 2022-06-02 14:13:16
* @LastEditTime: 2022-06-02 14:17:35
* @LastEditors: vyron
* @Description: 获取全局对象
* @FilePath: /utils/packages/utils/src/object/global.ts
*/

const _globalThis =
typeof globalThis === 'object' &&
globalThis !== null &&
globalThis.Object === Object &&
globalThis

const _self =
typeof self === 'object' && self !== null && self.Object === Object && self

const _global =
typeof global === 'object' &&
global !== null &&
global.Object === Object &&
global

export function getGlobalThis(): typeof globalThis {
return _globalThis || _global || _self || Function('return this')()
}
3 changes: 2 additions & 1 deletion packages/utils/src/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* @Author: vyron
* @Date: 2022-05-20 14:56:24
* @LastEditTime: 2022-05-24 14:16:44
* @LastEditTime: 2022-06-02 14:31:57
* @LastEditors: vyron
* @Description: common utils
* @FilePath: /utils/packages/utils/src/object/index.ts
*/
import { isArray, isLength, isNil, isNumber, isObject, isSymbol } from '../is'
export { getGlobalThis } from './global'

type PropertyName = string | number | symbol
type ObjectTarget = {
Expand Down
18 changes: 18 additions & 0 deletions packages/utils/src/string/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @Author: vyron
* @Date: 2022-06-01 16:03:12
* @LastEditTime: 2022-06-01 17:09:47
* @LastEditors: vyron
* @Description: string utils
* @FilePath: /utils/packages/utils/src/string/index.ts
*/

/**
* Converts the first character of a string to uppercase and returns the string
* @param {string} str - The string to convert
* @returns {string} - The converted string
*/
export function upperFirst(str: string): string {
if (!str) return str
return str.charAt(0).toUpperCase() + str.slice(1)
}
Loading

0 comments on commit da9bf29

Please sign in to comment.