Skip to content

Commit 4191fd1

Browse files
committed
initial
0 parents  commit 4191fd1

11 files changed

+6912
-0
lines changed

.babelrc.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
module.exports = api => ({
3+
presets: [
4+
[
5+
'@4c/4catalyzer',
6+
{
7+
target: 'web',
8+
modules: api.env() === 'esm' ? false : 'commonjs'
9+
},
10+
],
11+
]
12+
});
13+

.eslintignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lib/
2+
es/
3+
**/node_modules/**
4+
**/fixtures/**
5+
**/package.json
6+
**/CHANGELOG.md

.eslintrc

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"extends": ["4catalyzer-react", "prettier"],
3+
"plugins": ["prettier"],
4+
"rules": {
5+
"prettier/prettier": "error"
6+
},
7+
"env": {
8+
"browser": true
9+
},
10+
"overrides": [
11+
{
12+
"files": ["test/**"],
13+
"plugins": ["jest"],
14+
"env": {
15+
"jest/globals": true
16+
},
17+
"rules": {
18+
"global-require": "off",
19+
"no-await-in-loop": "off",
20+
"no-console": "off",
21+
"import/no-dynamic-require": "off",
22+
"jest/no-disabled-tests": "warn",
23+
"jest/no-focused-tests": "error",
24+
"jest/no-identical-title": "error",
25+
"jest/prefer-to-have-length": "warn",
26+
"jest/valid-expect": "error",
27+
"react/no-multi-comp": "off"
28+
}
29+
}
30+
]
31+
}

.gitignore

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
lib/
2+
es/
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
23+
# nyc test coverage
24+
.nyc_output
25+
26+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
27+
.grunt
28+
29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
32+
# node-waf configuration
33+
.lock-wscript
34+
35+
# Compiled binary addons (http://nodejs.org/api/addons.html)
36+
build/Release
37+
38+
# Dependency directories
39+
node_modules/
40+
jspm_packages/
41+
42+
# Typescript v1 declaration files
43+
typings/
44+
45+
# Optional npm cache directory
46+
.npm
47+
48+
# Optional eslint cache
49+
.eslintcache
50+
51+
# Optional REPL history
52+
.node_repl_history
53+
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# dotenv environment variables file
61+
.env
62+

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sudo: false
2+
3+
language: node_js
4+
node_js:
5+
- '9'
6+
- '8'
7+
after_script:
8+
- yarn codecov
9+
10+
after_success:
11+
- yarn semantic-release
12+
13+
cache: yarn
14+
branches:
15+
only:
16+
- master

package.json

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"name": "@4c/context",
3+
"version": "0.0.0-development",
4+
"main": "lib/index.js",
5+
"modules": "es/index.js",
6+
"jsnext:main": "es/index.js",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/4Catalyzer/context.git"
10+
},
11+
"author": "4Catalyzer",
12+
"license": "MIT",
13+
"scripts": {
14+
"tdd": "jest --watch",
15+
"test": "npm run lint && jest",
16+
"testonly": "jest",
17+
"build:es": "babel src -d es --env-name esm --ignore **/__tests__ --delete-dir-on-start",
18+
"build:lib": "babel src -d lib --ignore **/__tests__ --delete-dir-on-start",
19+
"build": "npm run build:lib && npm run build:es",
20+
"prepublishOnly": "yarn run build",
21+
"lint": "eslint . && prettier --list-different --ignore-path .eslintignore '**/*.{json,css,md}'",
22+
"format": "eslint . --fix && prettier --write --ignore-path .eslintignore '**/*.{json,css,md}'",
23+
"precommit": "lint-staged",
24+
"travis-deploy-once": "travis-deploy-once",
25+
"semantic-release": "semantic-release"
26+
},
27+
"publishConfig": {
28+
"access": "public"
29+
},
30+
"prettier": {
31+
"printWidth": 79,
32+
"singleQuote": true,
33+
"trailingComma": "all"
34+
},
35+
"lint-staged": {
36+
"*.js": [
37+
"eslint --fix",
38+
"git add"
39+
],
40+
"*.{json,css,md}": [
41+
"prettier --write --ignore-path .eslintignore",
42+
"git add"
43+
]
44+
},
45+
"jest": {
46+
"roots": [
47+
"<rootDir>/test"
48+
],
49+
"testEnvironment": "jsdom",
50+
"setupFiles": [
51+
"<rootDir>/test/index.js"
52+
]
53+
},
54+
"release": {
55+
"extends": [
56+
"@4c/semantic-release-config"
57+
]
58+
},
59+
"devDependencies": {
60+
"@4c/babel-preset-4catalyzer": "^1.0.0",
61+
"@4c/semantic-release-config": "^1.0.2",
62+
"@babel/cli": "^7.0.0-beta.39",
63+
"@babel/core": "^7.0.0-beta.39",
64+
"@monastic.panic/enzyme-adapter-react-16": "^1.2.2",
65+
"babel-core": "bridge",
66+
"babel-eslint": "^8.2.1",
67+
"babel-jest": "^22.4.3",
68+
"codecov": "^3.0.2",
69+
"enzyme": "^3.3.0",
70+
"eslint": "^4.16.0",
71+
"eslint-config-4catalyzer-react": "^0.4.1",
72+
"eslint-config-prettier": "^2.9.0",
73+
"eslint-plugin-import": "^2.8.0",
74+
"eslint-plugin-jest": "^21.7.0",
75+
"eslint-plugin-jsx-a11y": "^6.0.3",
76+
"eslint-plugin-prettier": "^2.5.0",
77+
"eslint-plugin-react": "^7.5.1",
78+
"husky": "^0.14.3",
79+
"lint-staged": "^7.1.0",
80+
"prettier": "^1.10.2",
81+
"react": "^16.3.2",
82+
"react-dom": "^16.3.2",
83+
"semantic-release": "^15.4.0",
84+
"travis-deploy-once": "^5.0.0"
85+
},
86+
"peerDependencies": {
87+
"react": ">=16.3.2"
88+
}
89+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { mapContextToProps } from './mapContextToProps';

src/mapContextToProps.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
3+
const getDisplayName = Component => {
4+
const name =
5+
typeof Component === 'string'
6+
? Component
7+
: Component.name || Component.displayName;
8+
return name ? `ContextTransform(${name})` : 'ContextTransform';
9+
};
10+
11+
function $mapContextToProps(
12+
{
13+
consumers: maybeArrayOfConsumers,
14+
mapToProps,
15+
displayName,
16+
forwardRefAs = 'ref',
17+
},
18+
Component,
19+
) {
20+
let consumers = maybeArrayOfConsumers;
21+
if (!Array.isArray(maybeArrayOfConsumers)) {
22+
consumers = [maybeArrayOfConsumers];
23+
}
24+
25+
const SingleConsumer = consumers[0];
26+
function singleRender(props, ref) {
27+
const propsWithRef = { [forwardRefAs]: ref, ...props };
28+
return (
29+
<SingleConsumer>
30+
{value => (
31+
<Component {...propsWithRef} {...mapToProps(value, props)} />
32+
)}
33+
</SingleConsumer>
34+
);
35+
}
36+
37+
function multiRender(props, ref) {
38+
const propsWithRef = { [forwardRefAs]: ref, ...props };
39+
return consumers.reduceRight(
40+
(inner, Consumer) => (...args) => (
41+
<Consumer>{value => inner(...args, value)}</Consumer>
42+
),
43+
(...contexts) => (
44+
<Component {...propsWithRef} {...mapToProps(...contexts, props)} />
45+
),
46+
)();
47+
}
48+
49+
const contextTransform = consumers.length === 1 ? singleRender : multiRender;
50+
contextTransform.displayName = displayName || getDisplayName(Component);
51+
52+
return React.forwardRef(contextTransform);
53+
}
54+
55+
export default function mapContextToProps(maybeOpts, mapToProps, Component) {
56+
if (arguments.length === 2) return $mapContextToProps(maybeOpts, mapToProps);
57+
return $mapContextToProps({ consumers: maybeOpts, mapToProps }, Component);
58+
}

test/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configure } from 'enzyme';
2+
import Adapter from '@monastic.panic/enzyme-adapter-react-16';
3+
4+
configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)