Skip to content

Commit 6cf1d92

Browse files
refactor: migrate to TypeScript
BREAKING CHANGE: Iterator excludes `Comment` and CommonJS requires the `default` key.
1 parent 0e3446d commit 6cf1d92

19 files changed

+5762
-8748
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jobs:
1414
- name: Run ESLint
1515
run: npm run lint
1616

17-
- name: Lint TypeScript
18-
run: npm run lint:dts
17+
- name: Type check
18+
run: npm run lint:tsc
1919

2020
- name: Run unit tests
2121
run: npm run test:ci

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ jspm_packages
4141
.node_repl_history
4242

4343
# Build files
44-
build
45-
dist
44+
cjs/
45+
dist/
4646

4747
# Vim swap files
4848
*.swp

.prettierrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"singleQuote": true,
3-
"trailingComma": "none"
2+
"singleQuote": true
43
}

README.md

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
Parses inline style to object:
1212

1313
```js
14-
var parse = require('style-to-object');
14+
import parse from 'style-to-object';
15+
1516
parse('color: #C0FFEE; background: #BADA55;');
1617
```
1718

@@ -48,16 +49,18 @@ yarn add style-to-object
4849

4950
## Usage
5051

51-
Import the module:
52+
Import with ES Modules:
5253

5354
```js
54-
// CommonJS
55-
const parse = require('style-to-object');
56-
57-
// ES Modules
5855
import parse from 'style-to-object';
5956
```
6057

58+
Require with CommonJS:
59+
60+
```js
61+
const parse = require('style-to-object').default;
62+
```
63+
6164
Parse single declaration:
6265

6366
```js
@@ -127,15 +130,15 @@ parse('/*'); // throws Error
127130
If the 2nd argument is a function, then the parser will return `null`:
128131

129132
```js
130-
parse('color: #f00', function() {}); // null
133+
parse('color: #f00', () => {}); // null
131134
```
132135

133136
But the function will iterate through each declaration:
134137

135138
<!-- prettier-ignore-start -->
136139

137140
```js
138-
parse('color: #f00', function(name, value, declaration) {
141+
parse('color: #f00', (name, value, declaration) => {
139142
console.log(name); // 'color'
140143
console.log(value); // '#f00'
141144
console.log(declaration); // { type: 'declaration', property: 'color', value: '#f00' }
@@ -161,42 +164,14 @@ parse(style, iterator);
161164
console.log(output); // [['color', 'red'], ['background', 'blue']]
162165
```
163166

164-
## Testing
165-
166-
Run tests:
167-
168-
```sh
169-
npm test
170-
```
171-
172-
Run tests in watch mode:
173-
174-
```sh
175-
npm run test:watch
176-
```
177-
178-
Run tests with coverage:
179-
180-
```sh
181-
npm run test:coverage
182-
```
183-
184-
Lint files:
185-
186-
```sh
187-
npm run lint
188-
```
189-
190-
Fix lint errors:
167+
## Migration
191168

192-
```sh
193-
npm run lint:fix
194-
```
169+
### v1
195170

196-
Test TypeScript declaration file for style and correctness:
171+
Migrated to TypeScript. Iterator excludes `Comment`. CommonJS requires the `.default` key:
197172

198-
```sh
199-
npm run lint:dts
173+
```js
174+
const parse = require('style-to-object').default;
200175
```
201176

202177
## Release
Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const cases = [
1+
export const cases = [
22
// general
33
['display: inline-block;', { display: 'inline-block' }],
44
['color:red;', { color: 'red' }],
@@ -9,29 +9,29 @@ const cases = [
99
'font-size': '.75em',
1010
position: 'absolute',
1111
width: '33.3%',
12-
'z-index': '1337'
13-
}
12+
'z-index': '1337',
13+
},
1414
],
1515
[
1616
'font-family: "Goudy Bookletter 1911", Gill Sans Extrabold, sans-serif;',
1717
{
18-
'font-family': '"Goudy Bookletter 1911", Gill Sans Extrabold, sans-serif'
19-
}
18+
'font-family': '"Goudy Bookletter 1911", Gill Sans Extrabold, sans-serif',
19+
},
2020
],
2121

2222
// multiple of same property
2323
[
2424
'color:rgba(0,0,0,1);color:white;',
2525
{
26-
color: 'white'
27-
}
26+
color: 'white',
27+
},
2828
],
2929

3030
// missing semicolon
3131
['line-height: 42', { 'line-height': '42' }],
3232
[
3333
'font-style:italic; text-transform:uppercase',
34-
{ 'font-style': 'italic', 'text-transform': 'uppercase' }
34+
{ 'font-style': 'italic', 'text-transform': 'uppercase' },
3535
],
3636

3737
// extra whitespace
@@ -52,8 +52,8 @@ const cases = [
5252
'-webkit-border-top-right-radius': '5px',
5353
'-webkit-border-bottom-right-radius': '10px',
5454
'-webkit-border-bottom-left-radius': '5px',
55-
'border-radius': '10px 5px'
56-
}
55+
'border-radius': '10px 5px',
56+
},
5757
],
5858

5959
// text and url
@@ -62,14 +62,14 @@ const cases = [
6262
[
6363
'background-image: url("http://cdn.example.com/image.png?v=42");',
6464
{
65-
'background-image': 'url("http://cdn.example.com/image.png?v=42")'
66-
}
65+
'background-image': 'url("http://cdn.example.com/image.png?v=42")',
66+
},
6767
],
6868
[
6969
'background: #123456 url("https://foo.bar/image.png?v=2")',
7070
{
71-
background: '#123456 url("https://foo.bar/image.png?v=2")'
72-
}
71+
background: '#123456 url("https://foo.bar/image.png?v=2")',
72+
},
7373
],
7474

7575
// property prefix
@@ -79,16 +79,16 @@ const cases = [
7979
'-webkit-hyphens': 'auto',
8080
'-moz-hyphens': 'auto',
8181
'-ms-hyphens': 'auto',
82-
hyphens: 'auto'
83-
}
82+
hyphens: 'auto',
83+
},
8484
],
8585

8686
// value prefix
8787
[
8888
'display: -webkit-box; display: -ms-flexbox; display: -webkit-flex; display: flex;',
8989
{
90-
display: 'flex'
91-
}
90+
display: 'flex',
91+
},
9292
],
9393

9494
// comment
@@ -97,38 +97,38 @@ const cases = [
9797
'top: 0; /* comment */ bottom: 42rem;',
9898
{
9999
top: '0',
100-
bottom: '42rem'
101-
}
100+
bottom: '42rem',
101+
},
102102
],
103103
[
104104
` right: 0; /* comment */
105105
/* comment */ left: 42rem; `,
106106
{
107107
right: '0',
108-
left: '42rem'
109-
}
108+
left: '42rem',
109+
},
110110
],
111111

112112
// custom
113113
[
114114
'foo: bar;',
115115
{
116-
foo: 'bar'
117-
}
116+
foo: 'bar',
117+
},
118118
],
119-
['foo:bar; baz:qux', { foo: 'bar', baz: 'qux' }]
120-
];
119+
['foo:bar; baz:qux', { foo: 'bar', baz: 'qux' }],
120+
] as const;
121121

122-
const errors = [
122+
export const errors = [
123123
// property missing ':'
124124
'overflow',
125125
'1',
126126

127127
// end of comment missing
128-
'/* unclosed comment'
129-
];
128+
'/* unclosed comment',
129+
] as const;
130130

131-
const invalids = [
131+
export const invalids = [
132132
undefined,
133133
null,
134134
true,
@@ -149,11 +149,5 @@ const invalids = [
149149

150150
// comment
151151
'/* color: #f00; */',
152-
'/**/'
153-
];
154-
155-
module.exports = {
156-
cases,
157-
errors,
158-
invalids
159-
};
152+
'/**/',
153+
] as const;

__tests__/index.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import StyleToObject from '../index.mjs';
2+
import parse from '../esm/index.mjs';
33

4-
assert.strictEqual(typeof StyleToObject, 'function');
5-
assert.deepEqual(StyleToObject('foo: bar'), { foo: 'bar' });
4+
assert.strictEqual(typeof parse, 'function');
5+
assert.deepEqual(parse('foo: bar'), { foo: 'bar' });

__tests__/index.test.js renamed to __tests__/index.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const inlineStyleParser = require('inline-style-parser');
2-
const { cases, errors, invalids } = require('./fixtures');
3-
const parse = require('..');
1+
import inlineStyleParser from 'inline-style-parser';
2+
import { cases, errors, invalids } from './fixtures';
3+
import parse from '../src';
44

55
describe('valid cases', () => {
66
describe.each(cases)('when style=%p', (style, expected) => {
@@ -23,7 +23,7 @@ describe('error cases', () => {
2323
describe('invalid cases', () => {
2424
describe.each(invalids)('when style=%p', (style) => {
2525
it('returns null', () => {
26-
expect(parse(style)).toBe(null);
26+
expect(parse(style as string)).toBe(null);
2727
});
2828
});
2929
});
@@ -48,7 +48,6 @@ describe('iterator', () => {
4848
parse(style, (name, value, comment) => {
4949
expect(name).toBe(undefined);
5050
expect(value).toBe(undefined);
51-
expect(comment.comment).toBe(' color: #f00; ');
5251
expect(comment).toEqual(inlineStyleParser(style)[0]);
5352
});
5453
});

__tests__/types/index.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

esm/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import StyleToObject from '../cjs/index.js';
2+
3+
export default StyleToObject.default;

index.d.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)