mirror of
https://github.com/KevinMidboe/vue-js-modal.git
synced 2025-10-29 18:00:20 +00:00
Added a module to tokenize strings of format number_suffix, e.g. 100px
This commit is contained in:
@@ -96,7 +96,8 @@
|
|||||||
default: 600,
|
default: 600,
|
||||||
validator (value) {
|
validator (value) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return value.charAt(value.length-1) === '%' && !isNaN(parseFloat(value))
|
return value.charAt(value.length-1) === '%'
|
||||||
|
&& !isNaN(parseFloat(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
@@ -109,7 +110,8 @@
|
|||||||
default: 300,
|
default: 300,
|
||||||
validator (value) {
|
validator (value) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return value === 'auto' || (value.charAt(value.length-1) === '%' && !isNaN(parseFloat(value)))
|
return (value.charAt(value.length-1) === '%'
|
||||||
|
&& !isNaN(parseFloat(value)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
|
|||||||
73
src/floatStringParser.js
Normal file
73
src/floatStringParser.js
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
// Parses string with float number and suffix:
|
||||||
|
// "0.001" => { type: "px", value: 0.001 }
|
||||||
|
// "0.001px" => { type: "px", value: 0.001 }
|
||||||
|
// "0.1%" => { type: "px", value: 0.1 }
|
||||||
|
// "foo" => { type: "", value: "foo" }
|
||||||
|
|
||||||
|
var floatRegexp = '[-+]?[0-9]*\.?[0-9]+'
|
||||||
|
|
||||||
|
var types = [
|
||||||
|
{
|
||||||
|
name: 'px',
|
||||||
|
regexp: new RegExp(`^${floatRegexp}px\$`)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '%',
|
||||||
|
regexp: new RegExp(`^${floatRegexp}%\$`)
|
||||||
|
},
|
||||||
|
// Fallback option:
|
||||||
|
// If no suffix specified, assign to px
|
||||||
|
{
|
||||||
|
name: 'px',
|
||||||
|
regexp: new RegExp(`^${floatRegexp}\$`)
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
var getType = (value) => {
|
||||||
|
for (var i = 0; i < types.length; i++) {
|
||||||
|
let type = types[i]
|
||||||
|
if (type.regexp.test(value)) {
|
||||||
|
return {
|
||||||
|
type: type.name,
|
||||||
|
value: parseFloat(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: '',
|
||||||
|
value: value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var parse = (value) => {
|
||||||
|
switch (typeof value) {
|
||||||
|
case 'number':
|
||||||
|
return { type: 'px', value }
|
||||||
|
case 'string':
|
||||||
|
return getType(value)
|
||||||
|
default:
|
||||||
|
return { type: '', value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default parse
|
||||||
|
|
||||||
|
/// tests
|
||||||
|
/*
|
||||||
|
console.log(parse(10))
|
||||||
|
console.log(parse(10.10))
|
||||||
|
console.log(parse(-10))
|
||||||
|
|
||||||
|
console.log(parse('5%'))
|
||||||
|
console.log(parse('-5%'))
|
||||||
|
console.log(parse('5.123%'))
|
||||||
|
|
||||||
|
console.log(parse('5px'))
|
||||||
|
console.log(parse('-5px'))
|
||||||
|
console.log(parse('5.123px'))
|
||||||
|
|
||||||
|
console.log(parse("adasd%"))
|
||||||
|
console.log(parse(""))
|
||||||
|
console.log(parse("+2-3px")) // fails
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user