Skip to content

Commit

Permalink
feat: add startsWith, endsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 3, 2018
1 parent 55d6c63 commit 43826a0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/endsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 检查 `str` 是否以 `needle` 结尾。
*
* @param str 要检查的字符串
* @param needle 要检索的字符串
* @returns 是(true)或否(false)
*/
export default function endsWith(str: string, needle: string): boolean {
return str.slice(-needle.length) === needle
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as Disposer } from './Disposer'
export { default as endsWith } from './endsWith'
export { default as fill } from './fill'
export { default as formatCurrency } from './formatCurrency'
export { default as formatDate } from './formatDate'
Expand Down Expand Up @@ -48,6 +49,7 @@ export { default as reduce } from './reduce'
export { default as repeat } from './repeat'
export { default as result } from './result'
export { default as shuffle } from './shuffle'
export { default as startsWith } from './startsWith'
export { default as stopEventPropagation } from './stopEventPropagation'
export { default as storage } from './storage'
export { default as supportPassiveEventListener } from './supportPassiveEventListener'
Expand Down
10 changes: 10 additions & 0 deletions src/startsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 检查 `str` 是否以 `needle` 开头。
*
* @param str 要检查的字符串
* @param needle 要检索的字符串
* @returns 是(true)或否(false)
*/
export default function startsWith(str: string, needle: string): boolean {
return str.indexOf(needle) === 0
}
26 changes: 26 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ describe('isChineseIDCardNumber', () => {
})
test('不是', () => {
[
'2000',
'190101881101231',
'110101881301231',
'110101198811214398',
'11010119881101331a',
Expand Down Expand Up @@ -982,3 +984,27 @@ describe('formatDate', () => {
)
})
})

describe('startsWith', () => {
test('是', () => {
expect(vtils.startsWith('hello', 'he')).toBeTruthy()
expect(vtils.startsWith('☺===', '☺')).toBeTruthy()
expect(vtils.startsWith('455', '455')).toBeTruthy()
})
test('否', () => {
expect(vtils.startsWith('hello', 'o')).toBeFalsy()
expect(vtils.startsWith('☺===', '☺-')).toBeFalsy()
})
})

describe('endsWith', () => {
test('是', () => {
expect(vtils.endsWith('hello', 'o')).toBeTruthy()
expect(vtils.endsWith('☺===', '===')).toBeTruthy()
expect(vtils.endsWith('455', '455')).toBeTruthy()
})
test('否', () => {
expect(vtils.endsWith('hello', 'll')).toBeFalsy()
expect(vtils.endsWith('☺===', '=.=')).toBeFalsy()
})
})

0 comments on commit 43826a0

Please sign in to comment.