Skip to content

Commit 1e32b77

Browse files
committed
Init commit
1 parent 2871d21 commit 1e32b77

12 files changed

+430
-1
lines changed

.babelrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"development": {
4+
"presets": ["es2015"]
5+
},
6+
"test": {
7+
"presets": ["es2015"],
8+
"plugins": [
9+
["__coverage__", {"ignore": "test/"}]
10+
]
11+
}
12+
}
13+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
# TypeScript builds
40+
lib

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.8.0

README.md

+53-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
1-
# container-query-toolkit
1+
# container-query-toolkit
2+
3+
:wrench: Basic utilities to work with container query.
4+
5+
## Install
6+
7+
```
8+
npm i -S container-query-toolkit
9+
```
10+
11+
## Usage
12+
13+
```js
14+
const kit = require('container-query-toolkit');
15+
// or
16+
import * as kit from 'container-query-toolkit';
17+
// or
18+
import {matchQueries} from 'container-query-toolkit';
19+
// or
20+
import matchQueries from 'container-query-toolkit/lib/matchQueries';
21+
22+
const query = {
23+
a: {minWidth: 400, maxWidth: 500, minHeight: 400, maxHeight: 500},
24+
b: {minWidth: 500, maxWidth: 600, minHeight: 400, maxHeight: 500},
25+
c: {minWidth: 400, maxWidth: 500, minHeight: 500, maxHeight: 600},
26+
d: {minWidth: 500, maxWidth: 600, minHeight: 500, maxHeight: 600},
27+
};
28+
29+
const result1 = matchQueries(query)({width: 300, height: 300});
30+
expect(result1).toEqual({a: false, b: false, c: false, d: false});
31+
32+
const result2 = matchQueries(query)({width: 450, height: 450});
33+
expect(result2).toEqual({a: true, b: false, c: false, d: false});
34+
35+
const result3 = matchQueries(query)({width: 450, height: 550});
36+
expect(result3).toEqual({a: false, b: false, c: true, d: false});
37+
38+
const result4 = matchQueries(query)({width: 550, height: 450});
39+
expect(result4).toEqual({a: false, b: true, c: false, d: false});
40+
41+
const result5 = matchQueries(query)({width: 550, height: 550});
42+
expect(result5).toEqual({a: false, b: false, c: false, d: true});
43+
44+
const result6 = matchQueries(query)({width: 700, height: 700});
45+
expect(result6).toEqual({a: false, b: false, c: false, d: false});
46+
```
47+
48+
## API
49+
50+
### `matchQueries(rules)(contentSize)`
51+
52+
- `rules: {[key: string]: {minWidth?: number, maxWidth?: number, minHeight?: number, maxHeight?: number}}`
53+
- `contentSize: {height: number, width: number}`

config/webpack.config.base.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
module.exports = {
4+
devtool: 'source-map',
5+
module: {
6+
loaders: [
7+
{
8+
loader: 'babel',
9+
exclude: /node_modules/,
10+
test: /\.js$/
11+
}
12+
]
13+
}
14+
};

karma.conf.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const _ = require('lodash');
5+
const webpackConfig = require('./config/webpack.config.base');
6+
7+
const webpackModule = _.cloneDeep(webpackConfig.module);
8+
9+
webpackModule.preLoaders = [
10+
// transpile all files except testing sources with babel as usual
11+
{
12+
test: /\.js$/,
13+
exclude: [
14+
path.resolve('node_modules/')
15+
],
16+
loader: 'babel'
17+
},
18+
];
19+
20+
const coverageReporters = [
21+
{
22+
type: 'html',
23+
subdir: (browser) => `${browser.toLowerCase().replace(/ /g, '-')}-html`
24+
},
25+
{
26+
type: 'json',
27+
subdir: (browser) => `${browser.toLowerCase().replace(/ /g, '-')}-json`
28+
},
29+
];
30+
31+
if (!process.env.CI) {
32+
coverageReporters.push({
33+
type: 'text'
34+
});
35+
}
36+
37+
module.exports = function (config) {
38+
config.set({
39+
basePath: '.',
40+
frameworks: ['jasmine'],
41+
files: [
42+
'test/index.js'
43+
],
44+
exclude: [
45+
],
46+
preprocessors: {
47+
'test/index.js': ['webpack', 'sourcemap']
48+
},
49+
reporters: process.env.CI ? ['spec', 'coverage', 'saucelabs'] : ['spec', 'coverage'],
50+
port: 9876,
51+
colors: true,
52+
logLevel: config.LOG_INFO,
53+
autoWatch: true,
54+
browsers: process.env.CI ? Object.keys(customLaunchers) : ['Chrome'],
55+
singleRun: true,
56+
concurrency: 2, // SanceLabs free account for open source
57+
browserDisconnectTolerance: 3,
58+
59+
// customLaunchers: customLaunchers,
60+
61+
// Webpack preprocessor
62+
webpack: {
63+
devtool: 'inline-source-map',
64+
module: webpackModule,
65+
},
66+
67+
// Coverage
68+
coverageReporter: {
69+
reporters: coverageReporters
70+
},
71+
72+
// Saucelabs launcher
73+
sauceLabs: {
74+
testName: 'react-container-query',
75+
public: 'public'
76+
},
77+
});
78+
};

package.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "container-query-toolkit",
3+
"version": "0.0.0",
4+
"description": "Basic utilities to work with container query.",
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/d6u/container-query-toolkit.git"
8+
},
9+
"keywords": [],
10+
"author": "Daiwei Lu <[email protected]> (http://daiwei.lu/)",
11+
"license": "MIT",
12+
"bugs": {
13+
"url": "https://github.com/d6u/container-query-toolkit/issues"
14+
},
15+
"homepage": "https://github.com/d6u/container-query-toolkit#readme",
16+
"main": "lib/index.js",
17+
"typings": "lib/index.d.ts",
18+
"files": [
19+
"lib"
20+
],
21+
"dependencies": {
22+
"element-resize-detector": "1.1.9",
23+
"lodash": "4.16.4"
24+
},
25+
"devDependencies": {
26+
"@types/lodash": "4.14.37",
27+
"babel-core": "6.17.0",
28+
"babel-loader": "6.2.5",
29+
"babel-plugin-__coverage__": "11.0.0",
30+
"babel-preset-es2015": "6.16.0",
31+
"babel-preset-es2015-loose": "8.0.0",
32+
"jasmine-core": "2.5.2",
33+
"karma": "1.3.0",
34+
"karma-babel-preprocessor": "6.0.1",
35+
"karma-chrome-launcher": "2.0.0",
36+
"karma-coverage": "1.1.1",
37+
"karma-jasmine": "1.0.2",
38+
"karma-sauce-launcher": "1.0.0",
39+
"karma-sourcemap-loader": "0.3.7",
40+
"karma-spec-reporter": "0.0.26",
41+
"karma-webpack": "1.8.0",
42+
"typescript": "2.0.3",
43+
"webpack": "1.13.2"
44+
},
45+
"scripts": {
46+
"clean": "rm -rf lib coverage",
47+
"build": "tsc",
48+
"test": "karma start",
49+
"preversion": "npm run clean && npm run build && npm test",
50+
"postversion": "git push && git push --tags"
51+
}
52+
}

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './matchQueries';

src/matchQueries.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import map = require('lodash/map');
2+
import toPairs = require('lodash/toPairs');
3+
4+
export interface Rules {
5+
[key: string]: {
6+
minWidth?: number;
7+
maxWidth?: number;
8+
minHeight?: number;
9+
maxHeight?: number;
10+
};
11+
}
12+
13+
/**
14+
* [matchQueries description]
15+
*/
16+
export default function matchQueries(rules: Rules) {
17+
const entries = map(toPairs(rules), ([className, rule]) => ({
18+
minWidth: rule.minWidth != null ? rule.minWidth : 0,
19+
maxWidth: rule.maxWidth != null ? rule.maxWidth : Infinity,
20+
minHeight: rule.minHeight != null ? rule.minHeight : 0,
21+
maxHeight: rule.maxHeight != null ? rule.maxHeight : Infinity,
22+
className: className
23+
}));
24+
25+
return function ({height, width}: {height: number, width: number}) {
26+
const classNameMap: {[key: string]: boolean} = {};
27+
28+
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+
);
35+
}
36+
37+
return classNameMap;
38+
};
39+
}

test/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
require('./matchQueries');

0 commit comments

Comments
 (0)