Skip to content

Can pass optional width or height #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,22 @@ expect(result5).toEqual({a: false, b: false, c: false, d: true});

const result6 = matchQueries(query)({width: 700, height: 700});
expect(result6).toEqual({a: false, b: false, c: false, d: false});

// {min|max}Height would be ignored if height is not provided.
const result7 = matchQueries(query)({width: 450});
expect(result7).toEqual({a: true, b: false, c: true, d: false});

// {min|max}Width would be ignored if width is not provided.
const result8 = matchQueries(query)({height: 450});
expect(result8).toEqual({a: true, b: true, c: false, d: false});
```

## API

### `matchQueries(rules)(contentSize)`

- `rules: {[key: string]: {minWidth?: number, maxWidth?: number, minHeight?: number, maxHeight?: number}}`
- `contentSize: {height: number, width: number}`

- `contentSize: {height?: number, width?: number}`

If `contentSize` is missing `height` or `width`, `{min|max}Height` or `{min|max}Width` rules will be ignored respectively.
20 changes: 13 additions & 7 deletions src/matchQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ export default function matchQueries(rules: Rules) {
className: className
}));

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

for (const {className, minWidth, maxWidth, minHeight, maxHeight} of entries) {
classNameMap[className] = (
minWidth <= width &&
width <= maxWidth &&
minHeight <= height &&
height <= maxHeight
);
if (height != null && width != null) {
classNameMap[className] = (
minWidth <= width && width <= maxWidth &&
minHeight <= height && height <= maxHeight
);
} else if (height == null && width != null) {
classNameMap[className] = minWidth <= width && width <= maxWidth;
} else if (height != null && width == null) {
classNameMap[className] = minHeight <= height && height <= maxHeight;
} else {
classNameMap[className] = true;
}
}

return classNameMap;
Expand Down
50 changes: 50 additions & 0 deletions test/matchQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,54 @@ describe('matchQueries', function () {

});

describe('no height info', function () {

it('ignores rules of height', function () {
const query = {
a: {minWidth: 400, maxWidth: 500, minHeight: 400, maxHeight: 500},
b: {minWidth: 500, maxWidth: 600, minHeight: 400, maxHeight: 500},
c: {minWidth: 400, maxWidth: 500, minHeight: 500, maxHeight: 600},
d: {minWidth: 500, maxWidth: 600, minHeight: 500, maxHeight: 600},
};

const result1 = matchQueries(query)({width: 300});
expect(result1).toEqual({a: false, b: false, c: false, d: false});

const result2 = matchQueries(query)({width: 450});
expect(result2).toEqual({a: true, b: false, c: true, d: false});

const result5 = matchQueries(query)({width: 550});
expect(result5).toEqual({a: false, b: true, c: false, d: true});

const result6 = matchQueries(query)({width: 700});
expect(result6).toEqual({a: false, b: false, c: false, d: false});
});

});

describe('no width info', function () {

it('ignores rules of width', function () {
const query = {
a: {minWidth: 400, maxWidth: 500, minHeight: 400, maxHeight: 500},
b: {minWidth: 500, maxWidth: 600, minHeight: 400, maxHeight: 500},
c: {minWidth: 400, maxWidth: 500, minHeight: 500, maxHeight: 600},
d: {minWidth: 500, maxWidth: 600, minHeight: 500, maxHeight: 600},
};

const result1 = matchQueries(query)({height: 300});
expect(result1).toEqual({a: false, b: false, c: false, d: false});

const result2 = matchQueries(query)({height: 450});
expect(result2).toEqual({a: true, b: true, c: false, d: false});

const result3 = matchQueries(query)({height: 550});
expect(result3).toEqual({a: false, b: false, c: true, d: true});

const result6 = matchQueries(query)({height: 700});
expect(result6).toEqual({a: false, b: false, c: false, d: false});
});

});

});