Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
utils
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontEnd
utils
Commits
8ec6fe2b
Commit
8ec6fe2b
authored
Feb 21, 2019
by
member
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update utils.js
parent
3a334936
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
158 additions
and
2 deletions
+158
-2
utils.js
utils.js
+158
-2
No files found.
utils.js
View file @
8ec6fe2b
...
...
@@ -53,4 +53,161 @@ export function toArray(list, start) {
while
(
i
--
)
{
ret
[
i
]
=
list
[
i
+
start
];
}
}
\ No newline at end of file
}
const
_isDev
=
process
.
env
.
NODE_ENV
===
'development'
;
/**
* 开发输出log
* @param {消息} msg
*/
export
const
log
=
msg
=>
{
// eslint-disable-next-line
if
(
_isDev
&&
console
&&
console
.
log
)
{
// eslint-disable-next-line
console
.
log
(
msg
);
}
};
// 防抖
export
function
debounce
(
fn
,
delay
)
{
delay
=
delay
||
200
;
let
timer
;
return
function
()
{
let
that
=
this
;
let
args
=
arguments
;
if
(
timer
)
{
clearTimeout
(
timer
);
}
timer
=
setTimeout
(
function
()
{
timer
=
null
;
fn
.
apply
(
that
,
args
);
},
delay
);
};
}
// 节流
export
function
throttle
(
fn
,
interval
)
{
let
last
;
let
timer
;
interval
=
interval
||
200
;
return
function
()
{
let
that
=
this
;
let
args
=
arguments
;
let
now
=
+
new
Date
();
if
(
last
&&
now
-
last
<
interval
)
{
clearTimeout
(
timer
);
timer
=
setTimeout
(
function
()
{
last
=
now
;
fn
.
apply
(
that
,
args
);
},
interval
);
}
else
{
last
=
now
;
fn
.
apply
(
that
,
args
);
}
};
}
/*
* 一个汉字算两个字符,一个英文字母或数字算一个字符
*/
export
const
getByteLen
=
val
=>
{
let
len
=
0
;
for
(
let
i
=
0
;
i
<
val
.
length
;
i
++
)
{
let
a
=
val
.
charAt
(
i
);
if
(
a
.
match
(
/
[^\x
00-
\x
ff
]
/gi
)
!=
null
)
{
len
+=
2
;
}
else
{
len
+=
1
;
}
}
return
len
;
};
/*
* 一个汉字算一个字,一个英文字母或数字算半个字
*/
export
const
getZhLen
=
val
=>
{
let
len
=
0
;
for
(
let
i
=
0
;
i
<
val
.
length
;
i
++
)
{
let
a
=
val
.
charAt
(
i
);
if
(
a
.
match
(
/
[^\x
00-
\x
ff
]
/gi
)
!=
null
)
{
len
+=
1
;
}
else
{
len
+=
0.5
;
}
}
return
Math
.
ceil
(
len
);
};
/*暂无用*/
export
const
cutStr
=
(
str
,
len
,
type
)
=>
{
let
char_length
=
0
;
for
(
let
i
=
0
;
i
<
str
.
length
;
i
++
)
{
let
son_str
=
str
.
charAt
(
i
);
if
(
type
==
1
)
{
encodeURI
(
son_str
).
length
>
2
?
(
char_length
+=
1
)
:
(
char_length
+=
0.5
);
}
if
(
type
==
2
)
{
char_length
+=
1
;
}
if
(
char_length
>=
len
)
{
let
sub_len
=
char_length
==
len
?
i
+
1
:
i
;
return
str
.
substr
(
0
,
sub_len
);
}
}
};
/*
* 一个汉字算一个字,两个英文/字母算一个字
*/
export
const
getByteVal2
=
val
=>
{
let
returnValue
=
''
;
// eslint-disable-next-line
let
byteValLen
=
0
;
for
(
let
i
=
0
;
i
<
val
.
length
;
i
++
)
{
if
(
val
[
i
].
match
(
/
[^\x
00-
\x
ff
]
/gi
)
!=
null
)
{
byteValLen
+=
1
;
}
else
{
byteValLen
+=
0.5
;
}
returnValue
+=
val
[
i
];
}
return
returnValue
;
};
/*
* 限制字数用, 一个汉字算一个字,两个英文/字母算一个字
*/
export
const
getByteVal
=
(
val
,
max
)
=>
{
let
returnValue
=
''
;
let
byteValLen
=
0
;
for
(
let
i
=
0
;
i
<
val
.
length
;
i
++
)
{
if
(
val
[
i
].
match
(
/
[^\x
00-
\x
ff
]
/gi
)
!=
null
)
byteValLen
+=
1
;
else
byteValLen
+=
0.5
;
if
(
byteValLen
>
max
)
break
;
returnValue
+=
val
[
i
];
}
return
returnValue
;
};
/*
* 限制字符数用, 一个汉字算两个字符,一个英文/字母算一个字符
*/
export
const
getCharVal
=
(
val
,
max
)
=>
{
let
returnValue
=
''
;
let
byteValLen
=
0
;
for
(
let
i
=
0
;
i
<
val
.
length
;
i
++
)
{
if
(
val
[
i
].
match
(
/
[^\x
00-
\x
ff
]
/gi
)
!=
null
)
byteValLen
+=
2
;
else
byteValLen
+=
1
;
if
(
byteValLen
>
max
)
break
;
returnValue
+=
val
[
i
];
}
return
returnValue
;
};
/*
* 正则校验,校验非负数字
*/
export
const
regPos
=
v
=>
{
let
regTest
=
/^
\d
+
(\.\d
+
)?
$/
;
return
regTest
.
test
(
v
);
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment