From 5fedb66a29d872433e651e68a86844640836e986 Mon Sep 17 00:00:00 2001 From: Adrien LAUER Date: Wed, 11 Jul 2018 14:02:06 +0200 Subject: [PATCH] Update to Lodash 4 --- modules/culture.js | 11 ++++--- modules/security.js | 59 +++++++++++++++++++------------------- modules/ui.js | 23 +++++++++------ package.json | 4 +-- specs/notification.spec.js | 2 +- specs/ui.spec.js | 10 ++++++- yarn.lock | 6 +--- 7 files changed, 65 insertions(+), 50 deletions(-) diff --git a/modules/culture.js b/modules/culture.js index 71fa0bb..5e8d3dc 100644 --- a/modules/culture.js +++ b/modules/culture.js @@ -21,7 +21,8 @@ define([ 'use strict'; // Config var config = module && module.config() || {}, - translationFallback = config.translationFallback || false; + translationFallback = config.translationFallback || false, + includes = _.contains || _.includes; // Global state var availableCultures = [], @@ -129,7 +130,9 @@ define([ var standardCalendar = culture.calendars.standard, currency = culture.numberFormat.currency, number = culture.numberFormat, - eras = standardCalendar.eras.map(function (era) { return era.name; }); + eras = standardCalendar.eras.map(function (era) { + return era.name; + }); function buildNumberPattern(patterns) { return { @@ -1307,8 +1310,8 @@ define([ }), function (elt) { return '{globalize}/cultures/globalize.culture.' + elt; }), function () { - availableCultures = _.pluck(_.filter(globalize.cultures, function (elt, key) { - return key !== 'default' && (key !== 'en' || _.contains(config.available, 'en')); + availableCultures = _.map(_.filter(globalize.cultures, function (elt, key) { + return key !== 'default' && (key !== 'en' || includes(config.available, 'en')); }), 'name'); availableCultureObjects = _.map(availableCultures, function (name) { diff --git a/modules/security.js b/modules/security.js index 29e8987..2f9a934 100644 --- a/modules/security.js +++ b/modules/security.js @@ -94,7 +94,8 @@ define([ var w20CoreSecurity = angular.module('w20CoreSecurity', ['w20CoreEnv', 'ngResource']), config = module && module.config() || {}, allProviders = {}, - allRealms = {}; + allRealms = {}, + includes = _.contains || _.includes; var BasicAuthenticationProvider = ['$resource', '$window', '$q', function ($resource, $window, $q) { var AuthenticationResource, AuthorizationsResource, realm, authenticationUrl, clearCredentials; @@ -364,7 +365,7 @@ define([ return false; } - return _.all(authProviders, function (provider) { + return _.every(authProviders, function (provider) { return provider.isAuthentifiable(); }); }, @@ -395,8 +396,8 @@ define([ deferred = null; $log.info('subject ' + currentSubject.id + ' authenticated on realm(s): ' + subjects.map(function (elt) { - return elt.realm; - })); + return elt.realm; + })); /** * @ngdoc event @@ -523,8 +524,8 @@ define([ deferred = null; $log.info('subject ' + currentSubject.id + ' refreshed on realm(s): ' + subjects.map(function (elt) { - return elt.realm; - })); + return elt.realm; + })); /** * @ngdoc event @@ -640,7 +641,7 @@ define([ function checkWithAttributeFilter(attributes) { return !(_.keys(attributeFilter).length > 0 && !_.find(attributeFilter, function (valueToCheck, attributeToCheck) { if (attributes && attributes[attributeToCheck]) { - return _.contains(attributes[attributeToCheck], '*') || _.contains(attributes[attributeToCheck], valueToCheck); + return includes(attributes[attributeToCheck], '*') || includes(attributes[attributeToCheck], valueToCheck); } else { return true; } @@ -757,7 +758,7 @@ define([ invalidAttributes = {}; _.each(value, function (attrValue, attrName) { - if (!_.contains(validAttributes[attrName], attrValue)) { + if (!includes(validAttributes[attrName], attrValue)) { invalidAttributes[attrName] = attrValue; } }); @@ -840,8 +841,8 @@ define([ } if (typeof attributes !== 'undefined') { - return _.all(attributes, function (value, attribute) { - return _.contains(roleDefinition.$attributes[attribute], value); + return _.every(attributes, function (value, attribute) { + return includes(roleDefinition.$attributes[attribute], value); }); } else { return true; @@ -870,20 +871,20 @@ define([ } if (values.length === 0) { - var result = _.any(parent.$roles, function (role) { - return checkWithRoleFilter(currentRoles[realm][role] && currentRoles[realm][role].$unifiedRole); - }) && checkWithAttributeFilter(parent.$attributes); + var result = _.some(parent.$roles, function (role) { + return checkWithRoleFilter(currentRoles[realm][role] && currentRoles[realm][role].$unifiedRole); + }) && checkWithAttributeFilter(parent.$attributes); if (typeof attributes === 'undefined') { return result; // no attribute check, return the result as-is } else { - return result && _.all(attributes, function (value, attribute) { - return _.contains(parent.$attributes[attribute], '*') || _.contains(parent.$attributes[attribute], value); - }); + return result && _.every(attributes, function (value, attribute) { + return includes(parent.$attributes[attribute], '*') || includes(parent.$attributes[attribute], value); + }); } } - return _.all(values.shift().split(','), function (value) { + return _.every(values.shift().split(','), function (value) { if (value.indexOf('$') === 0) { throw new Error('permissions cannot start with a $'); } @@ -992,20 +993,20 @@ define([ _.each(config.roleMapping, function (definition, name) { var mergedAttributes = {}; - if (_.all(definition, function (role, realm) { - var realRole = currentRoles[realm] && currentRoles[realm][role]; - if (realRole) { - _.map(realRole.$attributes, function (values, attribute) { - mergedAttributes[attribute] = _.union(values, mergedAttributes[attribute] || []); - }); + if (_.every(definition, function (role, realm) { + var realRole = currentRoles[realm] && currentRoles[realm][role]; + if (realRole) { + _.map(realRole.$attributes, function (values, attribute) { + mergedAttributes[attribute] = _.union(values, mergedAttributes[attribute] || []); + }); - realRole.$unifiedRole = name; + realRole.$unifiedRole = name; - return true; - } else { - return false; - } - })) { + return true; + } else { + return false; + } + })) { unifiedRoles[name] = { $attributes: mergedAttributes }; diff --git a/modules/ui.js b/modules/ui.js index 772d655..e683582 100644 --- a/modules/ui.js +++ b/modules/ui.js @@ -44,7 +44,8 @@ define([ */ var w20CoreUI = angular.module('w20CoreUI', ['w20CoreEnv', 'w20CoreSecurity', 'w20CoreCulture']), config = module && module.config() || {}, - allNavigation = {}; + allNavigation = {}, + includes = _.contains || _.includes; /** * @ngdoc service @@ -329,7 +330,7 @@ define([ _.each(keys, function (key) { while (key.lastIndexOf('.') > 0) { key = key.substring(0, key.lastIndexOf('.')); - if (!_.contains(keys, key)) { + if (!includes(keys, key)) { pathByCategory[key] = []; } } @@ -462,12 +463,18 @@ define([ if (!parentMenuTree) { return null; } - return _.sortBy(_.compact(_.filter(_.values(parentMenuTree), + return _.sortBy( + _.compact( + _.filter(_.values(parentMenuTree), function (elt) { + return _.isArray(elt); + }).concat(_.map(_.compact(parentMenuTree), 'route')) + ), function (elt) { - return _.isArray(elt); - }).concat(_.pluck(_.compact(parentMenuTree), 'route'))), - function (elt) { - return typeof elt.categoryPosition !== 'undefined' ? elt.categoryPosition : (typeof elt.sortKey !== 'undefined' ? elt.sortKey : elt); + if (_.isArray(elt)) { + return elt.categoryPosition || 0; + } else { + return 1000000 + elt.sortKey || 0; + } }); }, @@ -659,7 +666,7 @@ define([ } function getAll() { - return _.pluck(_.sortBy(_.filter(items, function (item) { + return _.map(_.sortBy(_.filter(items, function (item) { return !item.security || securityExpressionService.evaluate(item.security); }), 'sortKey'), 'name'); } diff --git a/package.json b/package.json index 96133eb..f4e4264 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@w20/w20", - "version": "2.4.5", + "version": "2.4.6", "description": "W20 Web framework", "license": "MPL-2.0", "contributors": [ @@ -43,7 +43,7 @@ "globalize": "^0.1.1", "jgrowl": "^1.4.6", "jquery": "^3.0.0", - "lodash": "^2.4.2", + "lodash": "^4.17.10", "require-css": "^0.1.10", "requirejs": "^2.1.22", "requirejs-text": "^2.0.15", diff --git a/specs/notification.spec.js b/specs/notification.spec.js index f18b3ce..8414311 100644 --- a/specs/notification.spec.js +++ b/specs/notification.spec.js @@ -73,7 +73,7 @@ define([ var notifications = notificationHistoryService.getNotifications(); - expect(_.pluck(notifications, 'id')).toEqual([1, 2, 3, 4, 5]); + expect(_.map(notifications, 'id')).toEqual([1, 2, 3, 4, 5]); }); it('should be able to get clear all notifications', function() { diff --git a/specs/ui.spec.js b/specs/ui.spec.js index b746b80..0e6de7e 100644 --- a/specs/ui.spec.js +++ b/specs/ui.spec.js @@ -158,6 +158,12 @@ define([ testRoute11: { category: 'category1.category11' }, + testRoute12: { + category: 'category1.category12' + }, + testRoute122: { + category: 'category1.category12' + }, testRouteInvisible: { category: 'invisible', hidden: true @@ -226,7 +232,9 @@ define([ subSubTree = navigationService.computeSubTree(subTree[0]); expect(subSubTree[0]).toEqual(jasmine.any(Array)); expect(subSubTree[0][0].category).toEqual('category1.category11'); - expect(subSubTree[1].category).toEqual('category1'); + expect(subSubTree[1][0].category).toEqual('category1.category12'); + expect(subSubTree[1][1].category).toEqual('category1.category12'); + expect(subSubTree[2].category).toEqual('category1'); subSubTree = navigationService.computeSubTree(subTree[1]); expect(subSubTree[0]).toEqual(jasmine.any(Array)); diff --git a/yarn.lock b/yarn.lock index 1ba37d3..8bb55de 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1665,11 +1665,7 @@ lodash@3.x, lodash@^3.10.1, lodash@^3.8.0: version "3.10.1" resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" -lodash@^2.4.2: - version "2.4.2" - resolved "https://registry.yarnpkg.com/lodash/-/lodash-2.4.2.tgz#fadd834b9683073da179b3eae6d9c0d15053f73e" - -lodash@^4.0.0, lodash@^4.0.1, lodash@^4.17.0, lodash@~4.17.10, lodash@~4.17.5: +lodash@^4.0.0, lodash@^4.0.1, lodash@^4.17.0, lodash@^4.17.10, lodash@~4.17.10, lodash@~4.17.5: version "4.17.10" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"