Skip to content

Commit

Permalink
fix: move string-length into utils (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
stkevintan authored and janryWang committed Jul 9, 2019
1 parent 5a9ea5a commit b84803b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/antd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"classnames": "^2.2.6",
"moveto": "^1.7.4",
"react-stikky": "^0.1.15",
"string-length": "^3.1.0",
"styled-components": "^4.1.1"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/antd/src/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { registerFormWrapper, registerFieldMiddleware } from '@uform/react'
import classNames from 'classnames'
import { Popover, Icon, Row, Col } from 'antd'
import styled from 'styled-components'
import stringLength from 'string-length'

import LOCALE from './locale'
import { isFn, moveTo, isStr } from './utils'
import { isFn, moveTo, isStr, stringLength } from './utils'
import { IFormItemProps, IFormProps } from './type'

/**
Expand Down
1 change: 0 additions & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"classnames": "^2.2.6",
"moveto": "^1.7.4",
"react-stikky": "^0.1.15",
"string-length": "^3.1.0",
"styled-components": "^4.1.1"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { ConfigProvider, Balloon, Icon } from '@alifd/next'
import { Row, Col } from '@alifd/next/lib/grid'
import LOCALE from './locale'
import styled from 'styled-components'
import { isFn, moveTo, isStr } from './utils'
import stringLength from 'string-length'
import { isFn, moveTo, isStr, stringLength } from './utils'

/**
* 轻量级Next Form,不包含任何数据管理能力
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './lru'
export * from './isEmpty'
export * from './case'
export * from './defer'
export * from './stringLength'
19 changes: 19 additions & 0 deletions packages/utils/src/stringLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ansiRegex
const ansiRegex = () => {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))'
].join('|')

return new RegExp(pattern, 'g')
}

// astralRegex
const regex = '[\uD800-\uDBFF][\uDC00-\uDFFF]'

const astralRegex = (opts?: { exact: boolean }) => opts && opts.exact ? new RegExp(`^${regex}$`) : new RegExp(regex, 'g')

// stripAnsi
const stripAnsi = <T extends any>(input:T): T => typeof input === 'string' ? input.replace(ansiRegex(), '') : input

export const stringLength = (input: string) => stripAnsi(input).replace(astralRegex(), ' ').length

0 comments on commit b84803b

Please sign in to comment.