Skip to content

Commit 8cd5b46

Browse files
committed
Add Rule unit values
1 parent 45c29af commit 8cd5b46

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

CSSObject/rules/Rule.js

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ICSS from "../enums/ICSS.js"
2+
import IUnit from "../enums/IUnit.js"
23
import FunctionRule from "./FunctionRule.js"
34

45

@@ -27,7 +28,7 @@ class Rule {
2728
set rule(value) {
2829
let [ prop, val ] = value
2930
this.property = prop
30-
this.value = this.values(val)
31+
this.value = this.__values(val)
3132
}
3233

3334
/**
@@ -48,23 +49,24 @@ class Rule {
4849
this.property = property
4950
}
5051

51-
5252
/**
5353
* get values of rule property
5454
* @param {string} values
5555
* @returns string[]
5656
*/
57-
values(values) {
57+
__values(values) {
5858
if (values.includes(ICSS.BRACKET.BEGIN)) {
5959
let params = this.__bracketParams(values)
60+
this.setUnit(values.replace(`(${params})`, ''))
6061
let val = params.reduce((a, p, i) => a.replace(`(${p})`, `#${i}`), values)
61-
let v = val.split(val.includes(ICSS.COMMA)? ICSS.COMMA:" ")
62+
let vals = val.split(val.includes(ICSS.COMMA)? ICSS.COMMA:" ")
6263

63-
return params.reduce((a, p, i) => a.replace(`#${i}`, `(${p})`), v.join('|'))
64+
return params
65+
.reduce((a, p, i) => a.replace(`#${i}`, `(${p})`), vals.join('|'))
6466
.split('|').map(v => this.value(v.trim()))
6567
}
6668

67-
return this.value(values)
69+
return this.setUnit(this.value(values))
6870
}
6971

7072
/**
@@ -73,29 +75,77 @@ class Rule {
7375
*/
7476
value(values) {
7577
if (this.__bracketParams(values).length > 1)
76-
return this.values(values)
78+
return this.__values(values)
7779

78-
if (values.includes(ICSS.BRACKET.END)) {
80+
if (values.includes(ICSS.BRACKET.END)) {
7981
let val = values.replace(/[)]/g, '')
8082
let [ name, value ] = val.split(/\((.+)/).map(v => v.trim())
8183
value = this.important(value)
8284
.split(!value.startsWith(ICSS.DATA_URI.KEY) && value.includes(ICSS.COMMA) ? ICSS.COMMA:" ")
8385
.filter(v => v != ICSS.EMPTY).map(v => v.trim())
8486

85-
return new FunctionRule(name, value)
86-
}
87+
return new FunctionRule(name, value)
88+
}
8789

8890
let val = this.important(values).split(ICSS.COMMA).map(v => v.trim())
8991
return val.length > 1 ? val:val.pop()
9092
}
9193

94+
/**
95+
* trait unit value
96+
* @param {string} values
97+
* @property unit
98+
* @returns number | string
99+
*/
100+
setUnit(values) {
101+
if (Array.isArray(values)) {
102+
let v = []
103+
values.map(values => {
104+
this.setUnit(values)
105+
v.push(this.__values)
106+
})
107+
108+
this.values = v
109+
} else {
110+
this.values = values.split(' ').map(v => {
111+
let value = parseFloat(v) ? parseFloat(v) : v == "0" ? 0 : v,
112+
unit = value == NaN ? IUnit.NO_UNIT : this.unity(v)
113+
114+
return { value, unit }
115+
})
116+
}
117+
118+
return values
119+
}
120+
121+
/**
122+
* has important declaration
123+
* @param {string} value
124+
* @property isImportant
125+
* @returns string
126+
*/
92127
important(value) {
93128
let i = value.split(ICSS.IMPORTANT)
94129
this.isImportant = (i.length > 1)? true:false
95130

96131
return (this.isImportant)? i.shift().trim():value
97132
}
98133

134+
/**
135+
* set property unit
136+
* @property unit
137+
* @param {string} value
138+
*/
139+
unity(value) {
140+
let u = value.replace(/[0-9]/g, ''),
141+
unity = IUnit.NO_UNIT;
142+
Object.entries(IUnit).map(([name, unit]) => {
143+
if (unit == u) unity = name
144+
})
145+
146+
return unity
147+
}
148+
99149
/**
100150
* get string between main brackets
101151
* @param {string} string

0 commit comments

Comments
 (0)