2
0
Эх сурвалжийг харах

first attempt to replace underscore with lodash

Richard Icke 8 жил өмнө
parent
commit
343e5777d1

+ 2 - 1
config.js

@@ -43,9 +43,10 @@ require.config({
         "text":                     "node_modules/text/text",
         "text":                     "node_modules/text/text",
         "tpl":                      "node_modules/requirejs-undertemplate/tpl",
         "tpl":                      "node_modules/requirejs-undertemplate/tpl",
         "typeahead":                "components/typeahead.js/index",
         "typeahead":                "components/typeahead.js/index",
+        "lodash":                   "node_modules/lodash/lodash",
         "underscore":               "node_modules/underscore/underscore",
         "underscore":               "node_modules/underscore/underscore",
         "utils":                    "src/utils",
         "utils":                    "src/utils",
-        
+
         // Converse
         // Converse
         "converse-api":             "src/converse-api",
         "converse-api":             "src/converse-api",
         "converse-bookmarks":       "src/converse-bookmarks",
         "converse-bookmarks":       "src/converse-bookmarks",

+ 1 - 1
docs/source/developer_guidelines.rst

@@ -112,7 +112,7 @@ Converse.js relies on the following dependencies:
 * `moment.js <http://momentjs.com/>`_ provides a better API for handling dates and times.
 * `moment.js <http://momentjs.com/>`_ provides a better API for handling dates and times.
 * `Strophe.js <http://strophe.im/>`_ maintains the XMPP session, is used to
 * `Strophe.js <http://strophe.im/>`_ maintains the XMPP session, is used to
   build XMPP stanzas, to send them, and to register handlers for received stanzas.
   build XMPP stanzas, to send them, and to register handlers for received stanzas.
-* `Underscore <http://underscorejs.org/>`_ provides very useful utility functions.
+* `lodash <https://lodash.com/>`_ provides very useful utility functions.
 * `Backbone <http://backbonejs.org/>`_ is used to model the data as Models and
 * `Backbone <http://backbonejs.org/>`_ is used to model the data as Models and
   Collections and to create Views that render the UI.
   Collections and to create Views that render the UI.
 * `backbone.overview <http://github.com/jcbrand/backbone.overview>`_ provides
 * `backbone.overview <http://github.com/jcbrand/backbone.overview>`_ provides

+ 1 - 1
docs/source/troubleshooting.rst

@@ -58,7 +58,7 @@ Solution:
 
 
 First, find out which object is referred to by ``Object [object Object]``.
 First, find out which object is referred to by ``Object [object Object]``.
 
 
-It will probably be the jQuery object ``$`` or perhaps the underscore.js object ``_``.
+It will probably be the jQuery object ``$`` or perhaps the lodash/underscore object ``_``.
 
 
 For the purpose of demonstration, I'm going to assume its ``$``, but the same
 For the purpose of demonstration, I'm going to assume its ``$``, but the same
 rules apply if its something else.
 rules apply if its something else.

+ 1 - 1
non_amd.html

@@ -46,7 +46,7 @@
     <!-- END STROPHE -->
     <!-- END STROPHE -->
 
 
     <!-- BEGIN BACKBONE -->
     <!-- BEGIN BACKBONE -->
-    <script type="text/javascript" src="node_modules/underscore/underscore.js"></script>
+    <script type="text/javascript" src="node_modules/lodash/core.js"></script>
     <script type="text/javascript" src="node_modules/backbone//backbone.js"></script>
     <script type="text/javascript" src="node_modules/backbone//backbone.js"></script>
     <script type="text/javascript" src="node_modules/backbone.browserStorage/backbone.browserStorage.js"></script>
     <script type="text/javascript" src="node_modules/backbone.browserStorage/backbone.browserStorage.js"></script>
     <script type="text/javascript" src="node_modules/backbone.overview/backbone.overview.js"></script>
     <script type="text/javascript" src="node_modules/backbone.overview/backbone.overview.js"></script>

+ 4 - 3
package.json

@@ -49,6 +49,8 @@
     "jquery-easing": "0.0.1",
     "jquery-easing": "0.0.1",
     "jquery.browser": ">=0.1.0",
     "jquery.browser": ">=0.1.0",
     "jshint": "^2.9.4",
     "jshint": "^2.9.4",
+    "lodash": "^4.17.4",
+    "lodash-cli": "^4.17.4",
     "moment": "~2.13.0",
     "moment": "~2.13.0",
     "otr": "0.2.16",
     "otr": "0.2.16",
     "phantom-jasmine": "0.1.8",
     "phantom-jasmine": "0.1.8",
@@ -57,11 +59,10 @@
     "po2json": "^0.4.4",
     "po2json": "^0.4.4",
     "requirejs": "2.3.2",
     "requirejs": "2.3.2",
     "requirejs-undertemplate": "~0.0.4",
     "requirejs-undertemplate": "~0.0.4",
-    "strophe.js": "1.2.12",
     "snyk": "^1.21.2",
     "snyk": "^1.21.2",
+    "strophe.js": "1.2.12",
     "strophejs-plugins": "0.0.7",
     "strophejs-plugins": "0.0.7",
-    "text": "requirejs/text#2.0.15",
-    "underscore": "~1.8.3"
+    "text": "requirejs/text#2.0.15"
   },
   },
   "dependencies": {}
   "dependencies": {}
 }
 }

+ 3 - 3
spec/bookmarks.js

@@ -2,15 +2,15 @@
     define([
     define([
         "jquery",
         "jquery",
         "converse-api",
         "converse-api",
-        "underscore",
         "utils",
         "utils",
         "mock",
         "mock",
         "test_utils"
         "test_utils"
         ], factory);
         ], factory);
-} (this, function ($, converse_api, _, utils, mock, test_utils) {
+} (this, function ($, converse_api, utils, mock, test_utils) {
     "use strict";
     "use strict";
     var $iq = converse_api.env.$iq,
     var $iq = converse_api.env.$iq,
-        Strophe = converse_api.env.Strophe;
+        Strophe = converse_api.env.Strophe,
+        _ = converse_api.env._;
 
 
     describe("A chat room", function () {
     describe("A chat room", function () {
 
 

+ 2 - 2
spec/converse.js

@@ -2,11 +2,11 @@
     define([
     define([
         "jquery",
         "jquery",
         "converse-api",
         "converse-api",
-        "underscore",
         "mock",
         "mock",
         "test_utils"], factory);
         "test_utils"], factory);
-} (this, function ($, converse_api, _, mock, test_utils) {
+} (this, function ($, converse_api, mock, test_utils) {
     var b64_sha1 = converse_api.env.b64_sha1;
     var b64_sha1 = converse_api.env.b64_sha1;
+    var _ = converse_api.env._;
 
 
     describe("Converse", function() {
     describe("Converse", function() {
         
         

+ 2 - 2
spec/transcripts.js

@@ -2,14 +2,14 @@
 (function (root, factory) {
 (function (root, factory) {
     define([
     define([
         "jquery",
         "jquery",
-        "underscore",
         "mock",
         "mock",
         "test_utils",
         "test_utils",
         "utils",
         "utils",
         "transcripts"
         "transcripts"
         ], factory
         ], factory
     );
     );
-} (this, function ($, _, mock, test_utils, utils, transcripts) {
+} (this, function ($, mock, test_utils, utils, transcripts) {
+    var _ = converse_api.env._;
     var Strophe = converse_api.env.Strophe;
     var Strophe = converse_api.env.Strophe;
     var IGNORED_TAGS = [
     var IGNORED_TAGS = [
         'stream:features',
         'stream:features',

+ 1 - 1
src/build-no-dependencies.js

@@ -36,7 +36,7 @@
         "strophe.ping",
         "strophe.ping",
         "typeahead",
         "typeahead",
         "otr",
         "otr",
-        "underscore"
+        "lodash"
     ],
     ],
     wrap: {
     wrap: {
         endFile: ["wrapper-no-jquery.js", "wrapper-no-deps.js"]
         endFile: ["wrapper-no-jquery.js", "wrapper-no-deps.js"]

+ 3 - 3
src/converse-api.js

@@ -8,7 +8,7 @@
 (function (root, factory) {
 (function (root, factory) {
     define("converse-api", [
     define("converse-api", [
             "jquery",
             "jquery",
-            "underscore",
+            "lodash",
             "moment_with_locales",
             "moment_with_locales",
             "strophe",
             "strophe",
             "utils",
             "utils",
@@ -75,10 +75,10 @@
             'set': function (key, val) {
             'set': function (key, val) {
                 var o = {};
                 var o = {};
                 if (typeof key === "object") {
                 if (typeof key === "object") {
-                    _.extend(converse, _.pick(key, Object.keys(converse.default_settings)));
+                    _.assignIn(converse, _.pick(key, Object.keys(converse.default_settings)));
                 } else if (typeof key === "string") {
                 } else if (typeof key === "string") {
                     o[key] = val;
                     o[key] = val;
-                    _.extend(converse, _.pick(o, Object.keys(converse.default_settings)));
+                    _.assignIn(converse, _.pick(o, Object.keys(converse.default_settings)));
                 }
                 }
             }
             }
         },
         },

+ 5 - 5
src/converse-bookmarks.js

@@ -12,7 +12,7 @@
 (function (root, factory) {
 (function (root, factory) {
     define("converse-bookmarks", [
     define("converse-bookmarks", [
             "jquery",
             "jquery",
-            "underscore",
+            "lodash",
             "moment_with_locales",
             "moment_with_locales",
             "strophe",
             "strophe",
             "utils",
             "utils",
@@ -78,7 +78,7 @@
                         var div = document.createElement('div');
                         var div = document.createElement('div');
                         div.innerHTML = html;
                         div.innerHTML = html;
                         var bookmark_button = converse.templates.chatroom_bookmark_toggle(
                         var bookmark_button = converse.templates.chatroom_bookmark_toggle(
-                            _.extend(
+                            _.assignIn(
                                 this.model.toJSON(),
                                 this.model.toJSON(),
                                 {
                                 {
                                     info_toggle_bookmark: __('Bookmark this room'),
                                     info_toggle_bookmark: __('Bookmark this room'),
@@ -173,7 +173,7 @@
                     if (!models.length) {
                     if (!models.length) {
                         this.renderBookmarkForm();
                         this.renderBookmarkForm();
                     } else {
                     } else {
-                        _.each(models, function (model) {
+                        _.forEach(models, function (model) {
                             model.destroy();
                             model.destroy();
                         });
                         });
                         this.$('.icon-pushpin').removeClass('button-on');
                         this.$('.icon-pushpin').removeClass('button-on');
@@ -324,7 +324,7 @@
                         'items[node="storage:bookmarks"] item[id="current"] storage conference'
                         'items[node="storage:bookmarks"] item[id="current"] storage conference'
                     );
                     );
                     var that = this;
                     var that = this;
-                    _.each(bookmarks, function (bookmark) {
+                    _.forEach(bookmarks, function (bookmark) {
                         that.create({
                         that.create({
                             'jid': bookmark.getAttribute('jid'),
                             'jid': bookmark.getAttribute('jid'),
                             'name': bookmark.getAttribute('name'),
                             'name': bookmark.getAttribute('name'),
@@ -391,7 +391,7 @@
                     var name = $(ev.target).data('bookmarkName');
                     var name = $(ev.target).data('bookmarkName');
                     var jid = $(ev.target).data('roomJid');
                     var jid = $(ev.target).data('roomJid');
                     if (confirm(__(___("Are you sure you want to remove the bookmark \"%1$s\"?"), name))) {
                     if (confirm(__(___("Are you sure you want to remove the bookmark \"%1$s\"?"), name))) {
-                        _.each(converse.bookmarks.where({'jid': jid}), function (item) { item.destroy(); });
+                        _.forEach(converse.bookmarks.where({'jid': jid}), function (item) { item.destroy(); });
                     }
                     }
                 },
                 },
 
 

+ 10 - 10
src/converse-core.js

@@ -9,7 +9,7 @@
 (function (root, factory) {
 (function (root, factory) {
     define("converse-core", [
     define("converse-core", [
         "jquery",
         "jquery",
-        "underscore",
+        "lodash",
         "polyfill",
         "polyfill",
         "utils",
         "utils",
         "moment_with_locales",
         "moment_with_locales",
@@ -34,7 +34,7 @@
     Strophe = Strophe.Strophe;
     Strophe = Strophe.Strophe;
 
 
     // Use Mustache style syntax for variable interpolation
     // Use Mustache style syntax for variable interpolation
-    /* Configuration of underscore templates (this config is distinct to the
+    /* Configuration of Lodash templates (this config is distinct to the
      * config of requirejs-tpl in main.js). This one is for normal inline templates.
      * config of requirejs-tpl in main.js). This one is for normal inline templates.
      */
      */
     _.templateSettings = {
     _.templateSettings = {
@@ -64,7 +64,7 @@
         },
         },
 
 
         on: function (evt, handler, context) {
         on: function (evt, handler, context) {
-            if (_.contains(['ready', 'initialized'], evt)) {
+            if (_.includes(['ready', 'initialized'], evt)) {
                 converse.log('Warning: The "'+evt+'" event has been deprecated and will be removed, please use "connected".');
                 converse.log('Warning: The "'+evt+'" event has been deprecated and will be removed, please use "connected".');
             }
             }
             if (context) {
             if (context) {
@@ -246,9 +246,9 @@
             xhr_custom_status: false,
             xhr_custom_status: false,
             xhr_custom_status_url: '',
             xhr_custom_status_url: '',
         };
         };
-        _.extend(this, this.default_settings);
+        _.assignIn(this, this.default_settings);
         // Allow only whitelisted configuration attributes to be overwritten
         // Allow only whitelisted configuration attributes to be overwritten
-        _.extend(this, _.pick(settings, Object.keys(this.default_settings)));
+        _.assignIn(this, _.pick(settings, Object.keys(this.default_settings)));
 
 
         // BBB
         // BBB
         if (this.prebind === true) { this.authentication = converse.PREBIND; }
         if (this.prebind === true) { this.authentication = converse.PREBIND; }
@@ -753,7 +753,7 @@
                 var bare_jid = Strophe.getBareJidFromJid(jid);
                 var bare_jid = Strophe.getBareJidFromJid(jid);
                 var resource = Strophe.getResourceFromJid(jid);
                 var resource = Strophe.getResourceFromJid(jid);
                 attributes.jid = bare_jid;
                 attributes.jid = bare_jid;
-                this.set(_.extend({
+                this.set(_.assignIn({
                     'id': bare_jid,
                     'id': bare_jid,
                     'jid': bare_jid,
                     'jid': bare_jid,
                     'fullname': bare_jid,
                     'fullname': bare_jid,
@@ -985,7 +985,7 @@
                 name = _.isEmpty(name)? jid: name;
                 name = _.isEmpty(name)? jid: name;
                 this.sendContactAddIQ(jid, name, groups,
                 this.sendContactAddIQ(jid, name, groups,
                     function (iq) {
                     function (iq) {
-                        var contact = this.create(_.extend({
+                        var contact = this.create(_.assignIn({
                             ask: undefined,
                             ask: undefined,
                             fullname: name,
                             fullname: name,
                             groups: groups,
                             groups: groups,
@@ -1246,7 +1246,7 @@
 
 
         this.RosterGroup = Backbone.Model.extend({
         this.RosterGroup = Backbone.Model.extend({
             initialize: function (attributes, options) {
             initialize: function (attributes, options) {
-                this.set(_.extend({
+                this.set(_.assignIn({
                     description: DESC_GROUP_TOGGLE,
                     description: DESC_GROUP_TOGGLE,
                     state: converse.OPENED
                     state: converse.OPENED
                 }, attributes));
                 }, attributes));
@@ -1515,7 +1515,7 @@
                                     ' and allow_non_roster_messaging is set to false', 'error');
                                     ' and allow_non_roster_messaging is set to false', 'error');
                         return;
                         return;
                     }
                     }
-                    chatbox = this.create(_.extend({
+                    chatbox = this.create(_.assignIn({
                         'id': bare_jid,
                         'id': bare_jid,
                         'jid': bare_jid,
                         'jid': bare_jid,
                         'fullname': jid,
                         'fullname': jid,
@@ -1928,7 +1928,7 @@
             } else if (this.bosh_service_url) {
             } else if (this.bosh_service_url) {
                 this.connection = new Strophe.Connection(
                 this.connection = new Strophe.Connection(
                     this.bosh_service_url,
                     this.bosh_service_url,
-                    _.extend(this.connection_options, {'keepalive': this.keepalive})
+                    _.assignIn(this.connection_options, {'keepalive': this.keepalive})
                 );
                 );
             } else {
             } else {
                 throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
                 throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");

+ 3 - 3
src/utils.js

@@ -3,7 +3,7 @@
     define([
     define([
         "jquery",
         "jquery",
         "jquery.browser",
         "jquery.browser",
-        "underscore",
+        "lodash",
         "tpl!field",
         "tpl!field",
         "tpl!select_option",
         "tpl!select_option",
         "tpl!form_select",
         "tpl!form_select",
@@ -78,7 +78,7 @@
                     }
                     }
                 }
                 }
                 $obj.html(x);
                 $obj.html(x);
-                _.each(list, function (url) {
+                _.forEach(list, function (url) {
                     isImage(url).then(function (ev) {
                     isImage(url).then(function (ev) {
                         var prot = url.indexOf('http://') === 0 || url.indexOf('https://') === 0 ? '' : 'http://';
                         var prot = url.indexOf('http://') === 0 || url.indexOf('https://') === 0 ? '' : 'http://';
                         var escaped_url = encodeURI(decodeURI(url)).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
                         var escaped_url = encodeURI(decodeURI(url)).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
@@ -325,7 +325,7 @@
             return function (item) {
             return function (item) {
                 if (typeof attr === 'object') {
                 if (typeof attr === 'object') {
                     var value = false;
                     var value = false;
-                    _.each(attr, function (a) {
+                    _.forEach(attr, function (a) {
                         value = value || item.get(a).toLowerCase().indexOf(query.toLowerCase()) !== -1;
                         value = value || item.get(a).toLowerCase().indexOf(query.toLowerCase()) !== -1;
                     });
                     });
                     return value;
                     return value;

+ 1 - 1
src/wrapper-end.js

@@ -2,7 +2,7 @@
 define('jquery', [], function () { return jQuery; });
 define('jquery', [], function () { return jQuery; });
 define('jquery.browser', [], function () { return jQuery; });
 define('jquery.browser', [], function () { return jQuery; });
 define('typeahead', [], function () { return jQuery; });
 define('typeahead', [], function () { return jQuery; });
-define('underscore', [], function () { return _; });
+define('lodash', [], function () { return _; });
 define('moment_with_locales', [], function () { return moment; });
 define('moment_with_locales', [], function () { return moment; });
 define('strophe', [], function () {
 define('strophe', [], function () {
     return {
     return {

+ 1 - 1
src/wrapper-no-deps.js

@@ -1,7 +1,7 @@
 /*global jQuery, _, moment, Strophe, $build, $iq, $msg, $pres, SHA1, Base64, MD5, DSA, OTR */
 /*global jQuery, _, moment, Strophe, $build, $iq, $msg, $pres, SHA1, Base64, MD5, DSA, OTR */
 define('jquery.browser', [], function () { return jQuery; });
 define('jquery.browser', [], function () { return jQuery; });
 define('typeahead', [], function () { return jQuery; });
 define('typeahead', [], function () { return jQuery; });
-define('underscore', [], function () { return _; });
+define('lodash', [], function () { return _; });
 define('moment_with_locales', [], function () { return moment; });
 define('moment_with_locales', [], function () { return moment; });
 define('strophe', [], function () {
 define('strophe', [], function () {
     return {
     return {

+ 1 - 0
tests/mock.js

@@ -1,6 +1,7 @@
 (function (root, factory) {
 (function (root, factory) {
     define("mock", ['converse'], factory);
     define("mock", ['converse'], factory);
 }(this, function (converse_api) {
 }(this, function (converse_api) {
+    var _ = converse_api.env._;
     var Strophe = converse_api.env.Strophe;
     var Strophe = converse_api.env.Strophe;
     var $iq = converse_api.env.$iq;
     var $iq = converse_api.env.$iq;
     var mock = {};
     var mock = {};