Skip to content

Commit 023387d

Browse files
committed
refactor(lib): change import of lodash library
1 parent a1c0aab commit 023387d

File tree

1 file changed

+64
-45
lines changed

1 file changed

+64
-45
lines changed

lib/src/ngx-restangular-config.factory.ts

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
import * as _ from './lodash';
1+
import {
2+
includes,
3+
isUndefined,
4+
isNull,
5+
isArray,
6+
isObject,
7+
isBoolean,
8+
defaults,
9+
each,
10+
extend,
11+
find,
12+
has,
13+
initial,
14+
last,
15+
clone,
16+
reduce,
17+
keys,
18+
isEmpty,
19+
forEach,
20+
} from 'lodash';
221

322
export function RestangularConfigurer(object, config){
423
object.configuration = config;
@@ -8,24 +27,24 @@ export function RestangularConfigurer(object, config){
827
*/
928
var safeMethods = ['get', 'head', 'options', 'trace', 'getlist'];
1029
config.isSafe = function (operation) {
11-
return _.includes(safeMethods, operation.toLowerCase());
30+
return includes(safeMethods, operation.toLowerCase());
1231
};
1332

1433
var absolutePattern = /^https?:\/\//i;
1534
config.isAbsoluteUrl = function (string) {
16-
return _.isUndefined(config.absoluteUrl) || _.isNull(config.absoluteUrl) ?
35+
return isUndefined(config.absoluteUrl) || isNull(config.absoluteUrl) ?
1736
string && absolutePattern.test(string) :
1837
config.absoluteUrl;
1938
};
2039

21-
config.absoluteUrl = _.isUndefined(config.absoluteUrl) ? true : config.absoluteUrl;
40+
config.absoluteUrl = isUndefined(config.absoluteUrl) ? true : config.absoluteUrl;
2241
object.setSelfLinkAbsoluteUrl = function (value) {
2342
config.absoluteUrl = value;
2443
};
2544
/**
2645
* This is the BaseURL to be used with Restangular
2746
*/
28-
config.baseUrl = _.isUndefined(config.baseUrl) ? '' : config.baseUrl;
47+
config.baseUrl = isUndefined(config.baseUrl) ? '' : config.baseUrl;
2948
object.setBaseUrl = function (newBaseUrl) {
3049
config.baseUrl = /\/$/.test(newBaseUrl) ?
3150
newBaseUrl.substring(0, newBaseUrl.length - 1) :
@@ -61,10 +80,10 @@ export function RestangularConfigurer(object, config){
6180
}
6281

6382
config.withHttpValues = function (httpLocalConfig, obj) {
64-
return _.defaults(obj, httpLocalConfig, config.defaultHttpFields);
83+
return defaults(obj, httpLocalConfig, config.defaultHttpFields);
6584
};
6685

67-
config.encodeIds = _.isUndefined(config.encodeIds) ? true : config.encodeIds;
86+
config.encodeIds = isUndefined(config.encodeIds) ? true : config.encodeIds;
6887
object.setEncodeIds = function (encode) {
6988
config.encodeIds = encode;
7089
};
@@ -80,8 +99,8 @@ export function RestangularConfigurer(object, config){
8099
object.setDefaultRequestParams = function (param1, param2) {
81100
var methods = [],
82101
params = param2 || param1;
83-
if (!_.isUndefined(param2)) {
84-
if (_.isArray(param1)) {
102+
if (!isUndefined(param2)) {
103+
if (isArray(param1)) {
85104
methods = param1;
86105
} else {
87106
methods.push(param1);
@@ -90,7 +109,7 @@ export function RestangularConfigurer(object, config){
90109
methods.push('common');
91110
}
92111

93-
_.each(methods, function (method) {
112+
each(methods, function (method) {
94113
config.defaultRequestParams[method] = params;
95114
});
96115
return this;
@@ -124,22 +143,22 @@ export function RestangularConfigurer(object, config){
124143
**/
125144
config.methodOverriders = config.methodOverriders || [];
126145
object.setMethodOverriders = function (values) {
127-
var overriders = _.extend([], values);
146+
var overriders = extend([], values);
128147
if (config.isOverridenMethod('delete', overriders)) {
129148
overriders.push('remove');
130149
}
131150
config.methodOverriders = overriders;
132151
return this;
133152
};
134153

135-
config.jsonp = _.isUndefined(config.jsonp) ? false : config.jsonp;
154+
config.jsonp = isUndefined(config.jsonp) ? false : config.jsonp;
136155
object.setJsonp = function (active) {
137156
config.jsonp = active;
138157
};
139158

140159
config.isOverridenMethod = function (method, values) {
141160
var search = values || config.methodOverriders;
142-
return !_.isUndefined(_.find(search, function (one: string) {
161+
return !isUndefined(find(search, function (one: string) {
143162
return one.toLowerCase() === method.toLowerCase();
144163
}));
145164
};
@@ -149,7 +168,7 @@ export function RestangularConfigurer(object, config){
149168
**/
150169
config.urlCreator = config.urlCreator || 'path';
151170
object.setUrlCreator = function (name) {
152-
if (!_.has(config.urlCreatorFactory, name)) {
171+
if (!has(config.urlCreatorFactory, name)) {
153172
throw new Error('URL Path selected isn\'t valid');
154173
}
155174

@@ -221,7 +240,7 @@ export function RestangularConfigurer(object, config){
221240
};
222241
object.setRestangularFields = function (resFields) {
223242
config.restangularFields =
224-
_.extend({}, config.restangularFields, resFields);
243+
extend({}, config.restangularFields, resFields);
225244
return this;
226245
};
227246

@@ -232,24 +251,24 @@ export function RestangularConfigurer(object, config){
232251
config.setFieldToElem = function (field, elem, value) {
233252
var properties = field.split('.');
234253
var idValue = elem;
235-
_.each(_.initial(properties), function (prop: any) {
254+
each(initial(properties), function (prop: any) {
236255
idValue[prop] = {};
237256
idValue = idValue[prop];
238257
});
239-
var index: any = _.last(properties);
258+
var index: any = last(properties);
240259
idValue[index] = value;
241260
return this;
242261
};
243262

244263
config.getFieldFromElem = function (field, elem) {
245264
var properties = field.split('.');
246265
var idValue: any = elem;
247-
_.each(properties, function (prop) {
266+
each(properties, function (prop) {
248267
if (idValue) {
249268
idValue = idValue[prop];
250269
}
251270
});
252-
return _.clone(idValue);
271+
return clone(idValue);
253272
};
254273

255274
config.setIdToElem = function (elem, id /*, route */) {
@@ -262,7 +281,7 @@ export function RestangularConfigurer(object, config){
262281
};
263282

264283
config.isValidId = function (elemId) {
265-
return '' !== elemId && !_.isUndefined(elemId) && !_.isNull(elemId);
284+
return '' !== elemId && !isUndefined(elemId) && !isNull(elemId);
266285
};
267286

268287
config.setUrlToElem = function (elem, url /*, route */) {
@@ -274,7 +293,7 @@ export function RestangularConfigurer(object, config){
274293
return config.getFieldFromElem(config.restangularFields.selfLink, elem);
275294
};
276295

277-
config.useCannonicalId = _.isUndefined(config.useCannonicalId) ? false : config.useCannonicalId;
296+
config.useCannonicalId = isUndefined(config.useCannonicalId) ? false : config.useCannonicalId;
278297
object.setUseCannonicalId = function (value) {
279298
config.useCannonicalId = value;
280299
return this;
@@ -301,10 +320,10 @@ export function RestangularConfigurer(object, config){
301320
};
302321

303322
config.responseExtractor = function (data, operation, what, url, response, subject) {
304-
var interceptors = _.clone(config.responseInterceptors);
323+
var interceptors = clone(config.responseInterceptors);
305324
interceptors.push(config.defaultResponseInterceptor);
306325
var theData = data;
307-
_.each(interceptors, function (interceptor: any) {
326+
each(interceptors, function (interceptor: any) {
308327
theData = interceptor(theData, operation,
309328
what, url, response, subject);
310329
});
@@ -346,12 +365,12 @@ export function RestangularConfigurer(object, config){
346365
};
347366

348367
config.fullRequestInterceptor = function (element, operation, path, url, headers, params, httpConfig) {
349-
var interceptors = _.clone(config.requestInterceptors);
368+
var interceptors = clone(config.requestInterceptors);
350369
var defaultRequest = config.defaultInterceptor(element, operation, path, url, headers, params, httpConfig);
351-
return _.reduce(interceptors, function (request: any, interceptor: any) {
370+
return reduce(interceptors, function (request: any, interceptor: any) {
352371

353372
let returnInterceptor: any = interceptor(request.element, operation, path, url, request.headers, request.params, request.httpConfig);
354-
return _.extend(request, returnInterceptor);
373+
return extend(request, returnInterceptor);
355374
}, defaultRequest);
356375
};
357376

@@ -408,11 +427,11 @@ export function RestangularConfigurer(object, config){
408427
return true;
409428
};
410429
object.setParentless = function (values) {
411-
if (_.isArray(values)) {
430+
if (isArray(values)) {
412431
config.shouldSaveParent = function (route) {
413-
return !_.includes(values, route);
432+
return !includes(values, route);
414433
};
415-
} else if (_.isBoolean(values)) {
434+
} else if (isBoolean(values)) {
416435
config.shouldSaveParent = function () {
417436
return !values;
418437
};
@@ -429,7 +448,7 @@ export function RestangularConfigurer(object, config){
429448
*
430449
* By default, the suffix is null
431450
*/
432-
config.suffix = _.isUndefined(config.suffix) ? null : config.suffix;
451+
config.suffix = isUndefined(config.suffix) ? null : config.suffix;
433452
object.setRequestSuffix = function (newSuffix) {
434453
config.suffix = newSuffix;
435454
return this;
@@ -455,7 +474,7 @@ export function RestangularConfigurer(object, config){
455474
}
456475

457476
typeTransformers.push(function (coll, elem) {
458-
if (_.isNull(isCollection) || (coll === isCollection)) {
477+
if (isNull(isCollection) || (coll === isCollection)) {
459478
return transformer(elem);
460479
}
461480
return elem;
@@ -479,22 +498,22 @@ export function RestangularConfigurer(object, config){
479498
var typeTransformers = config.transformers[route];
480499
var changedElem = elem;
481500
if (typeTransformers) {
482-
_.each(typeTransformers, function (transformer: (isCollection: boolean, changedElem: any) => any) {
501+
each(typeTransformers, function (transformer: (isCollection: boolean, changedElem: any) => any) {
483502
changedElem = transformer(isCollection, changedElem);
484503
});
485504
}
486505
return config.onElemRestangularized(changedElem, isCollection, route, Restangular);
487506
};
488507

489-
config.transformLocalElements = _.isUndefined(config.transformLocalElements) ?
508+
config.transformLocalElements = isUndefined(config.transformLocalElements) ?
490509
false :
491510
config.transformLocalElements;
492511

493512
object.setTransformOnlyServerElements = function (active) {
494513
config.transformLocalElements = !active;
495514
};
496515

497-
config.fullResponse = _.isUndefined(config.fullResponse) ? false : config.fullResponse;
516+
config.fullResponse = isUndefined(config.fullResponse) ? false : config.fullResponse;
498517
object.setFullResponse = function (full) {
499518
config.fullResponse = full;
500519
return this;
@@ -527,20 +546,20 @@ export function RestangularConfigurer(object, config){
527546

528547
function RestangularResource(config, $http, url, configurer) {
529548
var resource = {};
530-
_.each(_.keys(configurer), function (key) {
549+
each(keys(configurer), function (key) {
531550
var value = configurer[key];
532551

533552
// Add default parameters
534-
value.params = _.extend({}, value.params, config.defaultRequestParams[value.method.toLowerCase()]);
553+
value.params = extend({}, value.params, config.defaultRequestParams[value.method.toLowerCase()]);
535554
// We don't want the ? if no params are there
536-
if (_.isEmpty(value.params)) {
555+
if (isEmpty(value.params)) {
537556
delete value.params;
538557
}
539558

540559
if (config.isSafe(value.method)) {
541560

542561
resource[key] = function () {
543-
let config = _.extend(value, {
562+
let config = extend(value, {
544563
url: url
545564
});
546565
return $http.createRequest(config);
@@ -549,7 +568,7 @@ export function RestangularConfigurer(object, config){
549568
} else {
550569

551570
resource[key] = function (data) {
552-
let config = _.extend(value, {
571+
let config = extend(value, {
553572
url: url,
554573
data: data
555574
});
@@ -563,8 +582,8 @@ export function RestangularConfigurer(object, config){
563582
}
564583

565584
BaseCreator.prototype.resource = function (current, $http, localHttpConfig, callHeaders, callParams, what, etag, operation) {
566-
var params = _.defaults(callParams || {}, this.config.defaultRequestParams.common);
567-
var headers = _.defaults(callHeaders || {}, this.config.defaultHeaders);
585+
var params = defaults(callParams || {}, this.config.defaultRequestParams.common);
586+
var headers = defaults(callHeaders || {}, this.config.defaultHeaders);
568587

569588
if (etag) {
570589
if (!config.isSafe(operation)) {
@@ -683,7 +702,7 @@ export function RestangularConfigurer(object, config){
683702

684703
Path.prototype.base = function (current) {
685704
var __this = this;
686-
return _.reduce(this.parentsArray(current), function (acum: any, elem: any) {
705+
return reduce(this.parentsArray(current), function (acum: any, elem: any) {
687706
var elemUrl;
688707
var elemSelfLink = __this.config.getUrlFromElem(elem);
689708
if (elemSelfLink) {
@@ -769,12 +788,12 @@ export function RestangularConfigurer(object, config){
769788
if (value === null || value === undefined) {
770789
return;
771790
}
772-
if (!_.isArray(value)) {
791+
if (!isArray(value)) {
773792
value = [value];
774793
}
775794

776-
_.forEach(value, function (v) {
777-
if (_.isObject(v)) {
795+
forEach(value, function (v) {
796+
if (isObject(v)) {
778797
v = JSON.stringify(v);
779798
}
780799
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));

0 commit comments

Comments
 (0)