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

Shrink browser build by removing async and requiring Lodash piecemeal #76

Merged
merged 1 commit into from
Nov 28, 2018
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
22,932 changes: 4,891 additions & 18,041 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
15 changes: 9 additions & 6 deletions lib/object_to_query_param_string.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
'use strict';

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

// 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,11 +38,11 @@ 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) {
forEach(keys(obj), function(key) {
var value = obj[key];
buildParams(key, value, addFn);
});
Expand Down
56 changes: 33 additions & 23 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'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 forEach = require('lodash/forEach');
var map = require('lodash/map');
var keys = require('lodash/keys');

var check = require('./typecheck');
var Class = require('./class');
Expand All @@ -14,8 +23,8 @@ var Query = Class.extend({
* or `eachPage` is called.
*/
init: function(table, params) {
assert(_.isPlainObject(params));
_.each(_.keys(params), function(key) {
assert(isPlainObject(params));
forEach(keys(params), function(key) {
var value = params[key];
assert(Query.paramValidators[key] && Query.paramValidators[key](value).pass, 'Invalid parameter for Query: ' + key);
});
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,12 +174,13 @@ 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) {

forEach(keys(params), function(key) {
var value = params[key];
if (Query.paramValidators.hasOwnProperty(key)) {
var validator = Query.paramValidators[key];
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
38 changes: 20 additions & 18 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,21 @@ 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);
this._base.runAction('get', '/' + this._urlEncodedNameOrId() + '/', listRecordsParameters, null, function (err, response, results) {
if (err) {
done(err);
return;
}
], done);

var records = map(results.records, 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 +150,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
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"test": "jest --env node"
},
"dependencies": {
"async": "1.5.2",
"lodash": "4.17.10",
"request": "2.88.0",
"xhr": "2.3.3"
Expand Down