Skip to content
This repository was archived by the owner on Sep 29, 2024. It is now read-only.

Commit 9fb7c2d

Browse files
committed
Refactor for further compatibility and simplicity
1 parent 80d76f8 commit 9fb7c2d

File tree

7 files changed

+86
-41
lines changed

7 files changed

+86
-41
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm install css-typed-om
1616
Polyfill the `window` object:
1717

1818
```js
19-
import { polyfill } from 'css-typed-om';
19+
import polyfill from 'css-typed-om';
2020

2121
polyfill(window);
2222
```
@@ -85,20 +85,37 @@ exist:
8585

8686
### StylePropertyMap
8787

88-
A constructor for `StylePropertyMap`, used to polyfill
88+
A constructor for `StylePropertyMap`, used by the polyfill for `CSS`,
89+
`CSSRule`, and `Element`.
90+
91+
```js
92+
import { StylePropertyMap } from 'css-typed-om';
93+
```
8994

9095
### CSSKeywordValue
9196

9297
A constructor for `CSSKeywordValue`, used by `StylePropertyMap`.
9398

99+
```js
100+
import { CSSKeywordValue } from 'css-typed-om';
101+
```
102+
94103
### CSSUnitValue
95104

96105
A constructor for `CSSUnitValue`, used by `StylePropertyMap`.
97106

107+
```js
108+
import { CSSUnitValue } from 'css-typed-om';
109+
```
110+
98111
### CSSStyleValue
99112

100113
A constructor for `CSSStyleValue`, the super constructor of `CSSKeywordValue`
101-
and `CSSUnitValue`.
114+
and `CSSStyleValue`.
115+
116+
```js
117+
import { StylePropertyMap } from 'css-typed-om';
118+
```
102119

103120
[npm-url]: https://www.npmjs.com/package/css-typed-om
104121
[npm-img]: https://img.shields.io/npm/v/css-typed-om.svg

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import CSSStyleValue from './lib/CSSStyleValue';
44
import CSSUnitValue from './lib/CSSUnitValue';
55
import StylePropertyMap from './lib/StylePropertyMap';
66

7+
export default polyfill
8+
79
export {
8-
polyfill,
910
CSSKeywordValue,
1011
CSSStyleValue,
1112
CSSUnitValue,

lib/CSSKeywordValue.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
import CSSStyleValue from './CSSStyleValue';
22

3-
export default class CSSKeywordValue extends CSSStyleValue {
4-
get value() {
5-
return this._value;
6-
}
7-
8-
set value(value) {
9-
this._value = String(value)
10-
}
11-
3+
export default class CSSKeywordValue {
124
toString() {
13-
return `${this._value}`;
5+
return `${this.value}`;
146
}
157

168
constructor(...args) {
17-
super()
18-
199
if (args.length < 1) {
2010
throw new TypeError(`Failed to construct 'CSSKeywordValue': 1 arguments required, but only ${args.length} present.`);
2111
}
2212

23-
this._value = String(args[0]);
13+
let [ value ] = args;
14+
15+
this.value = String(value);
16+
17+
Object.defineProperties(this, {
18+
value: {
19+
configurable: true,
20+
enumerable: true,
21+
get() {
22+
return value
23+
},
24+
set(newValue) {
25+
value = String(newValue);
26+
}
27+
}
28+
});
2429
}
2530
}
31+
32+
CSSKeywordValue.prototype = Object.create(CSSStyleValue.prototype);

lib/CSSStyleValue.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export default class CSSStyleValue {}
1+
export default class CSSStyleValue {
2+
constructor() {
3+
throw new TypeError('Illegal constructor');
4+
}
5+
}

lib/CSSUnitValue.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
import CSSStyleValue from './CSSStyleValue';
22
import units from './units';
33

4-
export default class CSSUnitValue extends CSSStyleValue {
5-
get value() {
6-
return this._value;
7-
}
8-
9-
set value(value) {
10-
this._value = getFiniteNumber(value)
11-
}
12-
13-
get unit() {
14-
return this._unit;
15-
}
16-
17-
toString() {
18-
return this._unit === 'number' ? `${this._value}` : `${this._value}${units[this._unit]}`;
19-
}
20-
4+
export default class CSSUnitValue {
215
constructor(...args) {
22-
super()
23-
246
if (args.length < 2) {
257
throw new TypeError(`Failed to construct 'CSSUnitValue': 2 arguments required, but only ${args.length} present.`);
268
}
279

28-
this._value = getFiniteNumber(args[0]);
29-
this._unit = getUnit(args[1]);
10+
let [ value, unit ] = args;
11+
12+
value = getFiniteNumber(value);
13+
unit = getUnit(unit);
14+
15+
Object.defineProperties(this, {
16+
value: {
17+
configurable: true,
18+
enumerable: true,
19+
get() {
20+
return value
21+
},
22+
set(newValue) {
23+
value = getFiniteNumber(newValue);
24+
}
25+
},
26+
unit: {
27+
configurable: true,
28+
enumerable: true,
29+
get() {
30+
return unit
31+
}
32+
},
33+
toString: {
34+
configurable: true,
35+
enumerable: false,
36+
value() {
37+
return this.unit === 'number' ? `${this.value}` : `${this.value}${units[this.unit]}`;
38+
}
39+
}
40+
});
3041
}
3142
}
3243

44+
CSSUnitValue.prototype = Object.create(CSSStyleValue.prototype);
45+
3346
function getFiniteNumber(value) {
3447
if (isNaN(value) || Math.abs(value) === Infinity) {
3548
throw new TypeError(`Failed to set the 'value' property on 'CSSUnitValue': The provided double value is non-finite.`);

lib/parse-as-value.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default string => {
88
if (unitParsingMatch) {
99
const [, value, unit] = unitParsingMatch;
1010

11-
return new CSSUnitValue(value, unitKeys[unitValues.indexOf(unit)]);
11+
return new CSSUnitValue(value, unitKeys[unitValues.indexOf(unit || '')]);
1212
}
1313

1414
return new CSSKeywordValue(string);

lib/polyfill.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { CSSKeywordValue, CSSUnitValue, CSSStyleValue, StylePropertyMap } from 'css-typed-om';
1+
import CSSKeywordValue from './CSSKeywordValue';
2+
import CSSUnitValue from './CSSUnitValue';
3+
import CSSStyleValue from './CSSStyleValue';
4+
import StylePropertyMap from './StylePropertyMap';
25
import units from './units';
36

47
export default function polyfill(window) {

0 commit comments

Comments
 (0)