Skip to content
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

[fix] resolver of react-native loops require on ./ #13

Merged
merged 3 commits into from
Sep 27, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGELOG

- Use separate file for `Breakdancer` base class, allows React Native to resolve `Breakdancer`.

## 1.1.1

- Removed `react-native` from peerDependencies. See #11 #12
97 changes: 97 additions & 0 deletions breakdancer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Breakdancer is a simple breakpoint utility.
*
* @constructor
* @param {Object} specification Different breakpoints we need to know.
* @public
*/
export default class Breakdancer {
constructor(specification) {
//
// Setup our default values after the window has been set so we don't have
// any undefined references.
//
this.specification = this.normalize(specification);
}

/**
* Normalize the specification.
*
* @param {Array|Object} specification Different breakpoints we need to know.
* @returns {Array} List of media query specifications
* @private
*/
normalize(specification) {
if (Array.isArray(specification)) return specification;

return Object.keys(specification).reduce(function reduce(memo, key) {
var breakpoint = specification[key];

//
// If there is no name specified, use the key as name.
//
breakpoint.name = breakpoint.name || key;
memo.push(breakpoint);
return memo;
}, []);
}

/**
* Check if the setup has changed since we've last checked the real estate.
*
* @param {Object} viewport The view port specification.
* @returns {Boolean} True if the breakpoint for the viewport has changed.
* @public
*/
changed(viewport) {
var breakpoint = this.breakpoint;
this.breakpoint = this.currently(viewport);

return this.breakpoint !== breakpoint;
}

/**
* Check if a given specification matches our current set resolution.
*
* @param {Object} viewport The view port specification.
* @param {Object} specification The supplied specification.
* @returns {Boolean} True if viewport fits into the specification.
* @private
*/
matches(viewport, specification) {
viewport = viewport || this.viewport();

let matched = false;

if ('height' in specification) {
matched = viewport.height < specification.height;

if (!matched) return matched;
}

if ('width' in specification) {
matched = viewport.width < specification.width;
}

return matched;
}

/**
* Find out which breakpoint we're currently triggering.
*
* @param {Object} viewport The view port specification.
* @returns {String} The current breakpoint that we got triggered.
* @public
*/
currently(viewport) {
viewport = viewport || this.viewport();

for (var i = 0, l = this.specification.length; i < l; i++) {
var spec = this.specification[i];

if (this.matches(viewport, spec)) return spec.name;
}

return 'unknown';
}
}
108 changes: 12 additions & 96 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Breakdancer from './breakdancer';
import get from 'propget';

/**
Expand All @@ -21,38 +22,25 @@ const win = typeof window !== 'undefined' ? window : {
* @param {Object} windows Option window object reference.
* @public
*/
export default class Breakdancer {
export default class WebDancer extends Breakdancer {
constructor(specification, windows) {
this.window = windows || win;
super(specification);

//
// Setup our default values after the window has been set so we don't have
// any undefined references.
//
this.specification = this.normalize(specification);
this.window = windows || win;
this.breakpoint = this.currently();
}

/**
* Normalize the specification.
* Return the current view port.
*
* @param {Array|Object} specification Different breakpoints we need to know.
* @returns {Array} List of media query specifications
* @private
* @returns {Object} viewport
* @public
*/
normalize(specification) {
if (Array.isArray(specification)) return specification;

return Object.keys(specification).reduce(function reduce(memo, key) {
var breakpoint = specification[key];

//
// If there is no name specified, use the key as name.
//
breakpoint.name = breakpoint.name || key;
memo.push(breakpoint);
return memo;
}, []);
viewport() {
return {
height: this.height(),
width: this.width()
};
}

/**
Expand Down Expand Up @@ -80,76 +68,4 @@ export default class Breakdancer {
|| get(this.window, 'document.body.clientHeight')
|| 0;
}

/**
* Check if the setup has changed since we've last checked the real estate.
*
* @param {Object} viewport The view port specification.
* @returns {Boolean} True if the breakpoint for the viewport has changed.
* @public
*/
changed(viewport) {
var breakpoint = this.breakpoint;
this.breakpoint = this.currently(viewport);

return this.breakpoint !== breakpoint;
}

/**
* Return the current view port.
*
* @returns {Object} viewport
* @public
*/
viewport() {
return {
height: this.height(),
width: this.width()
};
}

/**
* Check if a given specification matches our current set resolution.
*
* @param {Object} viewport The view port specification.
* @param {Object} specification The supplied specification.
* @returns {Boolean} True if viewport fits into the specification.
* @private
*/
matches(viewport, specification) {
viewport = viewport || this.viewport();

let matched = false;

if ('height' in specification) {
matched = viewport.height < specification.height;

if (!matched) return matched;
}

if ('width' in specification) {
matched = viewport.width < specification.width;
}

return matched;
}

/**
* Find out which breakpoint we're currently triggering.
*
* @param {Object} viewport The view port specification.
* @returns {String} The current breakpoint that we got triggered.
* @public
*/
currently(viewport) {
viewport = viewport || this.viewport();

for (var i = 0, l = this.specification.length; i < l; i++) {
var spec = this.specification[i];

if (this.matches(viewport, spec)) return spec.name;
}

return 'unknown';
}
}
15 changes: 14 additions & 1 deletion index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import Breakdancer from './';
import Breakdancer from './breakdancer';
import { Dimensions } from 'react-native';

/**
* Breakdancer is a simple breakpoint utility.
*
* @constructor
* @param {Object} specification Different breakpoints we need to know.
* @public
*/
export default class NativeDancer extends Breakdancer {
constructor(specification) {
super(specification);

this.breakpoint = this.currently();
}

/**
* Return the current view port.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint": "eslint-godaddy *.js",
"test:web": "mocha --compilers js:babel-register ./test.js",
"test:native": "mocha --require react-native-mock/mock.js --compilers js:babel-register ./test.native.js",
"build": "babel index.js -d ./lib",
"build": "babel index.js breakdancer.js -d ./lib",
"prepublish": "npm run build",
"pretest": "npm run lint",
"test": "npm run test:web && npm run test:native"
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Breakdancer from './';
import Breakdancer from './index';
import assume from 'assume';

it('is exposed as a function', function () {
Expand Down