Skip to content

Commit

Permalink
Shrink browser build by removing async and requiring Lodash piecemeal
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Hahn committed Nov 28, 2018
1 parent 2dffb8b commit ad194d0
Show file tree
Hide file tree
Showing 10 changed files with 4,991 additions and 18,128 deletions.
22,952 changes: 4,900 additions & 18,052 deletions build/airtable.browser.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions lib/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _ = require('lodash');
var forEach = require('lodash/forEach');

var internalConfig = require('./internal_config.json');
var Class = require('./class');
var AirtableError = require('./airtable_error');
Expand Down Expand Up @@ -68,7 +69,7 @@ Base.createFunctor = function(airtable, baseId) {
var baseFn = function() {
return base.doCall.apply(base, arguments);
};
_.each(['table', 'runAction', 'getId'], function(baseMethod) {
forEach(['table', 'runAction', 'getId'], function(baseMethod) {
baseFn[baseMethod] = base[baseMethod].bind(base);
});
baseFn._base = base;
Expand Down
2 changes: 1 addition & 1 deletion lib/callback_to_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
function callbackToPromise(fn, context, callbackArgIndex) {
return function() {
// If callbackArgIndex isn't provided, use the last argument.
if (callbackArgIndex === undefined) {
if (callbackArgIndex === void 0) {
callbackArgIndex = arguments.length > 0 ? arguments.length - 1 : 0;
}
var callbackArg = arguments[callbackArgIndex];
Expand Down
22 changes: 13 additions & 9 deletions lib/object_to_query_param_string.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use strict';

var _ = require('lodash');
var isArray = require('lodash/isArray');
var forEach = require('lodash/forEach');
var isNil = require('lodash/isNil');

// Adapted from jQuery.param:
// https://github.com/jquery/jquery/blob/2.2-stable/src/serialize.js
function buildParams(prefix, obj, addFn) {
var name;
if (_.isArray(obj)) {
if (isArray(obj)) {
// Serialize array item.
_.each(obj, function(value, index) {
forEach(obj, function(value, index) {
if (/\[\]$/.test(prefix)) {
// Treat each array item as a scalar.
addFn(prefix, value);
} else {
// Item is non-scalar (array or object), encode its numeric index.
buildParams(
prefix + '[' + (typeof value === 'object' && value !== null && value !== undefined ? index : '') + ']',
prefix + '[' + (typeof value === 'object' && value !== null ? index : '') + ']',
value,
addFn
);
Expand All @@ -35,14 +37,16 @@ function buildParams(prefix, obj, addFn) {
function objectToQueryParamString(obj) {
var parts = [];
var addFn = function(key, value) {
value = (value === null || value === undefined) ? '' : value;
value = isNil(value) ? '' : value;
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
};

_.each(_.keys(obj), function(key) {
var value = obj[key];
buildParams(key, value, addFn);
});
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
buildParams(key, value, addFn);
}
}

return parts.join('&').replace(/%20/g, '+');
}
Expand Down
84 changes: 48 additions & 36 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
'use strict';

var assert = require('assert');
var _ = require('lodash');
var isPlainObject = require('lodash/isPlainObject');
var isArray = require('lodash/isArray');
var isFunction = require('lodash/isFunction');
var isString = require('lodash/isString');
var isNumber = require('lodash/isNumber');
var includes = require('lodash/includes');
var clone = require('lodash/clone');
var map = require('lodash/map');

var check = require('./typecheck');
var Class = require('./class');
Expand All @@ -14,11 +21,13 @@ var Query = Class.extend({
* or `eachPage` is called.
*/
init: function(table, params) {
assert(_.isPlainObject(params));
_.each(_.keys(params), function(key) {
var value = params[key];
assert(Query.paramValidators[key] && Query.paramValidators[key](value).pass, 'Invalid parameter for Query: ' + key);
});
assert(isPlainObject(params));
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
assert(Query.paramValidators[key] && Query.paramValidators[key](value).pass, 'Invalid parameter for Query: ' + key);
}
}

this._table = table;
this._params = params;
Expand All @@ -33,7 +42,7 @@ var Query = Class.extend({
* then calls `done(error, records)`.
*/
firstPage: function(done) {
assert(_.isFunction(done),
assert(isFunction(done),
'The first parameter to `firstPage` must be a function');

this.eachPage(function(records, fetchNextPage) {
Expand All @@ -54,15 +63,15 @@ var Query = Class.extend({
* `done(error)`.
*/
eachPage: function(pageCallback, done) {
assert(_.isFunction(pageCallback),
assert(isFunction(pageCallback),
'The first parameter to `eachPage` must be a function');

assert(_.isFunction(done) || _.isUndefined(done),
assert(isFunction(done) || (done === void 0),
'The second parameter to `eachPage` must be a function or undefined');

var that = this;
var path = '/' + this._table._urlEncodedNameOrId();
var params = _.clone(this._params);
var params = clone(this._params);

var inner = function() {
that._table._base.runAction('get', path, params, null, function(err, response, result) {
Expand All @@ -81,7 +90,7 @@ var Query = Class.extend({
};
}

var records = _.map(result.records, function(recordJson) {
var records = map(result.records, function(recordJson) {
return new Record(that._table, null, recordJson);
});

Expand All @@ -96,7 +105,7 @@ var Query = Class.extend({
* Fetches all pages of results asynchronously. May take a long time.
*/
all: function(done) {
assert(_.isFunction(done),
assert(isFunction(done),
'The first parameter to `all` must be a function');

var allRecords = [];
Expand All @@ -115,45 +124,45 @@ var Query = Class.extend({

Query.paramValidators = {
fields:
check(check.isArrayOf(_.isString), 'the value for `fields` should be an array of strings'),
check(check.isArrayOf(isString), 'the value for `fields` should be an array of strings'),

filterByFormula:
check(_.isString, 'the value for `filterByFormula` should be a string'),
check(isString, 'the value for `filterByFormula` should be a string'),

maxRecords:
check(_.isNumber, 'the value for `maxRecords` should be a number'),
check(isNumber, 'the value for `maxRecords` should be a number'),

pageSize:
check(_.isNumber, 'the value for `pageSize` should be a number'),
check(isNumber, 'the value for `pageSize` should be a number'),

sort:
check(check.isArrayOf(function(obj) {
return (
_.isPlainObject(obj) &&
_.isString(obj.field) &&
(_.isUndefined(obj.direction) || _.includes(['asc', 'desc'], obj.direction))
isPlainObject(obj) &&
isString(obj.field) &&
((obj.direction === void 0) || includes(['asc', 'desc'], obj.direction))
);
}), 'the value for `sort` should be an array of sort objects. ' +
'Each sort object must have a string `field` value, and an optional ' +
'`direction` value that is "asc" or "desc".'
),

view:
check(_.isString, 'the value for `view` should be a string'),
check(isString, 'the value for `view` should be a string'),

cellFormat:
check(function(cellFormat) {
return (
_.isString(cellFormat) &&
_.includes(['json', 'string'], cellFormat)
isString(cellFormat) &&
includes(['json', 'string'], cellFormat)
);
}, 'the value for `cellFormat` should be "json" or "string"'),

timeZone:
check(_.isString, 'the value for `timeZone` should be a string'),
check(isString, 'the value for `timeZone` should be a string'),

userLocale:
check(_.isString, 'the value for `userLocale` should be a string'),
check(isString, 'the value for `userLocale` should be a string'),
};

/**
Expand All @@ -165,25 +174,28 @@ Query.paramValidators = {
* errors: a list of error messages.
*/
Query.validateParams = function validateParams(params) {
assert(_.isPlainObject(params));
assert(isPlainObject(params));

var validParams = {};
var ignoredKeys = [];
var errors = [];
_.each(_.keys(params), function(key) {
var value = params[key];
if (Query.paramValidators.hasOwnProperty(key)) {
var validator = Query.paramValidators[key];
var validationResult = validator(value);
if (validationResult.pass) {
validParams[key] = value;

for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
if (Query.paramValidators.hasOwnProperty(key)) {
var validator = Query.paramValidators[key];
var validationResult = validator(value);
if (validationResult.pass) {
validParams[key] = value;
} else {
errors.push(validationResult.error);
}
} else {
errors.push(validationResult.error);
ignoredKeys.push(key);
}
} else {
ignoredKeys.push(key);
}
});
}

return {
validParams: validParams,
Expand Down
6 changes: 3 additions & 3 deletions lib/record.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var _ = require('lodash');
var assign = require('lodash/assign');

var Class = require('./class');
var callbackToPromise = require('./callback_to_promise');
Expand Down Expand Up @@ -38,7 +38,7 @@ var Record = Class.extend({
done = opts;
opts = {};
}
var updateBody = _.extend({
var updateBody = assign({
fields: cellValuesByName
}, opts);

Expand All @@ -55,7 +55,7 @@ var Record = Class.extend({
done = opts;
opts = {};
}
var updateBody = _.extend({
var updateBody = assign({
fields: cellValuesByName
}, opts);
this._table._base.runAction('put', '/' + this._table._urlEncodedNameOrId() + '/' + this.id, {}, updateBody, function(err, response, results) {
Expand Down
35 changes: 16 additions & 19 deletions lib/table.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var _ = require('lodash');
var isPlainObject = require('lodash/isPlainObject');
var assign = require('lodash/assign');
var forEach = require('lodash/forEach');
var map = require('lodash/map');

var assert = require('assert');
var async = require('async');

var AirtableError = require('./airtable_error');
var Class = require('./class');
Expand Down Expand Up @@ -40,7 +42,7 @@ var Table = Class.extend({
record.fetch(done);
},
_selectRecords: function(params) {
if (_.isUndefined(params)) {
if (params === void 0) {
params = {};
}

Expand All @@ -50,11 +52,11 @@ var Table = Class.extend({
'Use `eachPage` or `firstPage` to fetch records.');
}

if (_.isPlainObject(params)) {
if (isPlainObject(params)) {
var validationResults = Query.validateParams(params);

if (validationResults.errors.length) {
var formattedErrors = validationResults.errors.map(function(error) {
var formattedErrors = map(validationResults.errors, function(error) {
return ' * ' + error;
});

Expand All @@ -81,7 +83,7 @@ var Table = Class.extend({
done = optionalParameters;
optionalParameters = {};
}
var requestData = _.extend({fields: recordData}, optionalParameters);
var requestData = assign({fields: recordData}, optionalParameters);
this._base.runAction('post', '/' + that._urlEncodedNameOrId() + '/', {}, requestData, function(err, resp, body) {
if (err) { done(err); return; }

Expand Down Expand Up @@ -118,21 +120,16 @@ var Table = Class.extend({
done = opts;
opts = {};
}
var listRecordsParameters = _.extend({
var listRecordsParameters = assign({
limit: limit, offset: offset
}, opts);

async.waterfall([
function(next) {
that._base.runAction('get', '/' + that._urlEncodedNameOrId() + '/', listRecordsParameters, null, next);
},
function(response, results, next) {
var records = _.map(results.records, function(recordJson) {
return new Record(that, null, recordJson);
});
next(null, records, results.offset);
}
], done);
this._base.runAction('get', '/' + this._urlEncodedNameOrId() + '/', listRecordsParameters, null, function (response, results) {
var records = results.records.map(function(recordJson) {
return new Record(that, null, recordJson);
});
done(null, records, results.offset);
});
},
_forEachRecord: function(opts, callback, done) {
if (arguments.length === 2) {
Expand All @@ -148,7 +145,7 @@ var Table = Class.extend({
that._listRecords(limit, offset, opts, function(err, page, newOffset) {
if (err) { done(err); return; }

_.each(page, callback);
forEach(page, callback);

if (newOffset) {
offset = newOffset;
Expand Down
7 changes: 4 additions & 3 deletions lib/typecheck.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var _ = require('lodash');
var includes = require('lodash/includes');
var isArray = require('lodash/isArray');

function check(fn, error) {
return function(value) {
Expand All @@ -13,12 +14,12 @@ function check(fn, error) {
}

check.isOneOf = function isOneOf(options) {
return _.includes.bind(this, options);
return includes.bind(this, options);
};

check.isArrayOf = function(itemValidator) {
return function(value) {
return _.isArray(value) && _.every(value, itemValidator);
return isArray(value) && value.every(itemValidator);
};
};

Expand Down
Loading

0 comments on commit ad194d0

Please sign in to comment.