Skip to content

Commit 948407c

Browse files
authored
Can pass optional width or height (#2)
1 parent 1243cf2 commit 948407c

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@ expect(result5).toEqual({a: false, b: false, c: false, d: true});
4343

4444
const result6 = matchQueries(query)({width: 700, height: 700});
4545
expect(result6).toEqual({a: false, b: false, c: false, d: false});
46+
47+
// {min|max}Height would be ignored if height is not provided.
48+
const result7 = matchQueries(query)({width: 450});
49+
expect(result7).toEqual({a: true, b: false, c: true, d: false});
50+
51+
// {min|max}Width would be ignored if width is not provided.
52+
const result8 = matchQueries(query)({height: 450});
53+
expect(result8).toEqual({a: true, b: true, c: false, d: false});
4654
```
4755

4856
## API
4957

5058
### `matchQueries(rules)(contentSize)`
5159

5260
- `rules: {[key: string]: {minWidth?: number, maxWidth?: number, minHeight?: number, maxHeight?: number}}`
53-
- `contentSize: {height: number, width: number}`
61+
62+
- `contentSize: {height?: number, width?: number}`
63+
64+
If `contentSize` is missing `height` or `width`, `{min|max}Height` or `{min|max}Width` rules will be ignored respectively.

src/matchQueries.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ export default function matchQueries(rules: Rules) {
2222
className: className
2323
}));
2424

25-
return function ({height, width}: {height: number, width: number}) {
25+
return function ({height, width}: {height?: number, width?: number}) {
2626
const classNameMap: {[key: string]: boolean} = {};
2727

2828
for (const {className, minWidth, maxWidth, minHeight, maxHeight} of entries) {
29-
classNameMap[className] = (
30-
minWidth <= width &&
31-
width <= maxWidth &&
32-
minHeight <= height &&
33-
height <= maxHeight
34-
);
29+
if (height != null && width != null) {
30+
classNameMap[className] = (
31+
minWidth <= width && width <= maxWidth &&
32+
minHeight <= height && height <= maxHeight
33+
);
34+
} else if (height == null && width != null) {
35+
classNameMap[className] = minWidth <= width && width <= maxWidth;
36+
} else if (height != null && width == null) {
37+
classNameMap[className] = minHeight <= height && height <= maxHeight;
38+
} else {
39+
classNameMap[className] = true;
40+
}
3541
}
3642

3743
return classNameMap;

test/matchQueries.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,54 @@ describe('matchQueries', function () {
145145

146146
});
147147

148+
describe('no height info', function () {
149+
150+
it('ignores rules of height', function () {
151+
const query = {
152+
a: {minWidth: 400, maxWidth: 500, minHeight: 400, maxHeight: 500},
153+
b: {minWidth: 500, maxWidth: 600, minHeight: 400, maxHeight: 500},
154+
c: {minWidth: 400, maxWidth: 500, minHeight: 500, maxHeight: 600},
155+
d: {minWidth: 500, maxWidth: 600, minHeight: 500, maxHeight: 600},
156+
};
157+
158+
const result1 = matchQueries(query)({width: 300});
159+
expect(result1).toEqual({a: false, b: false, c: false, d: false});
160+
161+
const result2 = matchQueries(query)({width: 450});
162+
expect(result2).toEqual({a: true, b: false, c: true, d: false});
163+
164+
const result5 = matchQueries(query)({width: 550});
165+
expect(result5).toEqual({a: false, b: true, c: false, d: true});
166+
167+
const result6 = matchQueries(query)({width: 700});
168+
expect(result6).toEqual({a: false, b: false, c: false, d: false});
169+
});
170+
171+
});
172+
173+
describe('no width info', function () {
174+
175+
it('ignores rules of width', function () {
176+
const query = {
177+
a: {minWidth: 400, maxWidth: 500, minHeight: 400, maxHeight: 500},
178+
b: {minWidth: 500, maxWidth: 600, minHeight: 400, maxHeight: 500},
179+
c: {minWidth: 400, maxWidth: 500, minHeight: 500, maxHeight: 600},
180+
d: {minWidth: 500, maxWidth: 600, minHeight: 500, maxHeight: 600},
181+
};
182+
183+
const result1 = matchQueries(query)({height: 300});
184+
expect(result1).toEqual({a: false, b: false, c: false, d: false});
185+
186+
const result2 = matchQueries(query)({height: 450});
187+
expect(result2).toEqual({a: true, b: true, c: false, d: false});
188+
189+
const result3 = matchQueries(query)({height: 550});
190+
expect(result3).toEqual({a: false, b: false, c: true, d: true});
191+
192+
const result6 = matchQueries(query)({height: 700});
193+
expect(result6).toEqual({a: false, b: false, c: false, d: false});
194+
});
195+
196+
});
197+
148198
});

0 commit comments

Comments
 (0)