Skip to content

Commit b5fa628

Browse files
author
Amir Tocker
committed
Refactor utility library
1 parent a31cd7e commit b5fa628

File tree

6 files changed

+40
-18
lines changed

6 files changed

+40
-18
lines changed

src/Util/Util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import debounce from './debounce';
2+
import firstDefined from './firstDefined';
3+
import closestAbove from './closestAbove';
4+
5+
module.exports = {
6+
debounce, firstDefined, closestAbove
7+
};

src/Util/closestAbove.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Returns the first item in list that is greater or equal to the given value.
3+
* @param {Array} list a sorted array of items
4+
* @param {*} value
5+
* @returns {*}
6+
*/
7+
export default function closestAbove(list, value) {
8+
return list.reduce((last, next) => value <= last ? last : next, undefined);
9+
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
/**
2+
* Delay execution of func until wait miliseconds have passed since the last invocation of the returned debounced function.
3+
* @param {function} func the function to execute
4+
* @param {number} wait waiting time in miliseconds
5+
* @param {boolean=false} immediate if true execute func at the beginning of the wait period
6+
* @returns {function()} debounced function
7+
*/
18
export default function debounce(func, wait, immediate) {
29
let timeout = null;
3-
let debounced = () => {
10+
let debounced = () => {
411
const context = this;
512
const args = arguments;
613
const callNow = immediate && !timeout;
7-
if(timeout){
14+
if (timeout) {
815
clearTimeout(timeout);
916
}
1017
const later = () => {
@@ -20,10 +27,9 @@ export default function debounce(func, wait, immediate) {
2027
}
2128
};
2229

23-
debounced.cancel = ()=>{
30+
debounced.cancel = ()=> {
2431
clearTimeout(timeout);
2532
timeout = null;
2633
};
2734
return debounced;
28-
2935
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Returns the first argument that is not undefined
3+
* @param {...*} values a list of arguments of any type
4+
* @returns {*}
5+
*/
16
export default function firstDefined(...values){
27
for(let value of values) {
38
if(value !== undefined) return value;

src/Util/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "util",
3+
"version": "1.0.0",
4+
"main": "./Util.js",
5+
"private": true
6+
}

src/components/Image/Image.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, {Component, PropTypes} from 'react';
22
import cloudinary, {Util} from 'cloudinary-core';
33
import CloudinaryComponent from '../CloudinaryComponent';
4-
import debounce from '../../util/debounce';
5-
import firstDefined from '../../util/firstDefined';
4+
import {debounce, firstDefined, closestAbove} from '../../Util';
65

76
export default class Image extends CloudinaryComponent {
87
constructor(props, context) {
@@ -37,7 +36,7 @@ export default class Image extends CloudinaryComponent {
3736
}
3837

3938
let currentState = this.state || {};
40-
if (/* FIXME probably not needed */ Util.isEmpty(currentState.url) || url !== currentState.url) {
39+
if (!Util.isEmpty(url) && url !== currentState.url) {
4140
state.url = url;
4241
}
4342
return state;
@@ -66,7 +65,6 @@ export default class Image extends CloudinaryComponent {
6665
}
6766

6867
componentWillUpdate(nextProps, nextState, nextContext) {
69-
// TODO check responsive. also check for responsive state change.
7068
if (nextState.responsive) {
7169
const wait = firstDefined(nextProps.responsiveDebounce, nextContext.responsiveDebounce, 100);
7270
if (this.listener) {
@@ -105,7 +103,7 @@ export default class Image extends CloudinaryComponent {
105103
);
106104
}
107105

108-
// methods from cloudinary_js
106+
// Methods from cloudinary_js
109107

110108
findContainerWidth() {
111109
var containerWidth, style;
@@ -213,16 +211,7 @@ Image.defaultProps = {};
213211
Image.contextTypes = CloudinaryComponent.contextTypes;
214212
Image.propTypes = CloudinaryComponent.propTypes;
215213

216-
217214
function defaultBreakpoints(width, steps = 100) {
218215
return steps * Math.ceil(width / steps);
219216
}
220217

221-
function closestAbove(list, value) {
222-
var i;
223-
i = list.length - 2;
224-
while (i >= 0 && list[i] >= value) {
225-
i--;
226-
}
227-
return list[i + 1];
228-
}

0 commit comments

Comments
 (0)