Skip to content

Commit 9f16b45

Browse files
author
Nick Frasser
authored
Optimize class code to reduce file size (#147)
* Converting ES6 classes to regular ES5 with Object.create Feels like we're going a step backward here, but this significantly reduces the file size by taking away all the automatically-generated Babel class code. * Remove "xn-" domains from tlds.js There are a lot of these and they look pretty similar, so they'll be added in programmatically in a separate pull-request (or localization plugin) * Renaming class code files and format Also insuring it compiles to all platforms properly * Removing stray debugger
1 parent 4055310 commit 9f16b45

File tree

9 files changed

+112
-272
lines changed

9 files changed

+112
-272
lines changed

src/linkify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {inherits} from './linkify/utils/class';
12
import * as options from './linkify/utils/options';
23
import * as scanner from './linkify/core/scanner';
34
import * as parser from './linkify/core/parser';
@@ -60,4 +61,4 @@ let test = function (str, type=null) {
6061

6162
// Scanner and parser provide states and tokens for the lexicographic stage
6263
// (will be used to add additional link types)
63-
export {find, options, parser, scanner, test, tokenize};
64+
export {find, inherits, options, parser, scanner, test, tokenize};

src/linkify/core/state.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import {inherits} from '../utils/class';
2+
3+
function createStateClass() {
4+
return function (tClass) {
5+
this.j = [];
6+
this.T = tClass || null;
7+
};
8+
}
19

210
/**
311
A simple state machine that can emit token classes
@@ -18,17 +26,13 @@
1826
1927
@class BaseState
2028
*/
21-
class BaseState {
22-
29+
const BaseState = createStateClass();
30+
BaseState.prototype = {
2331
/**
2432
@method constructor
2533
@param {Class} tClass Pass in the kind of token to emit if there are
2634
no jumps after this state and the state is accepting.
2735
*/
28-
constructor(tClass) {
29-
this.j = [];
30-
this.T = tClass || null;
31-
}
3236

3337
/**
3438
On the given symbol(s), this machine should go to the given state
@@ -48,7 +52,7 @@ class BaseState {
4852
}
4953
this.j.push([symbol, state]);
5054
return this;
51-
}
55+
},
5256

5357
/**
5458
Given the next item, returns next state for that item
@@ -71,7 +75,7 @@ class BaseState {
7175

7276
// Nowhere left to jump!
7377
return false;
74-
}
78+
},
7579

7680
/**
7781
Does this state accept?
@@ -82,7 +86,7 @@ class BaseState {
8286
*/
8387
accepts() {
8488
return !!this.T;
85-
}
89+
},
8690

8791
/**
8892
Determine whether a given item "symbolizes" the symbol, where symbol is
@@ -97,7 +101,7 @@ class BaseState {
97101
*/
98102
test(item, symbol) {
99103
return item === symbol;
100-
}
104+
},
101105

102106
/**
103107
Emit the token for this State (just return it in this case)
@@ -108,17 +112,15 @@ class BaseState {
108112
emit() {
109113
return this.T;
110114
}
111-
}
112-
115+
};
113116

114117
/**
115118
State machine for string-based input
116119
117120
@class CharacterState
118121
@extends BaseState
119122
*/
120-
class CharacterState extends BaseState {
121-
123+
const CharacterState = inherits(BaseState, createStateClass(), {
122124
/**
123125
Does the given character match the given character or regular
124126
expression?
@@ -133,7 +135,7 @@ class CharacterState extends BaseState {
133135
charOrRegExp instanceof RegExp && charOrRegExp.test(character)
134136
);
135137
}
136-
}
138+
});
137139

138140

139141
/**
@@ -142,7 +144,7 @@ class CharacterState extends BaseState {
142144
@class TokenState
143145
@extends BaseState
144146
*/
145-
class TokenState extends BaseState {
147+
const TokenState = inherits(BaseState, createStateClass(), {
146148

147149
/**
148150
Is the given token an instance of the given token class?
@@ -155,7 +157,7 @@ class TokenState extends BaseState {
155157
test(token, tokenClass) {
156158
return token instanceof tokenClass;
157159
}
158-
}
160+
});
159161

160162
/**
161163
Given a non-empty target string, generates states (if required) for each

0 commit comments

Comments
 (0)