Use String#startsWith instead of String#indexOf

This commit is contained in:
Maximilian Krög
2022-08-04 23:59:17 +02:00
parent 0b945f2321
commit bb3c5bf144
14 changed files with 51 additions and 53 deletions

View File

@@ -14,7 +14,7 @@ if (data.type && data.type.names) {
<div class="anchor" id="<?js= id ?>"> <div class="anchor" id="<?js= id ?>">
</div> </div>
<h4 class="name"> <h4 class="name">
<?js= data.attribs + (data.scope === 'static' ? longname : name.indexOf('module:') === 0 ? name.split('/').pop() : name) + typeSignature ?> <?js= data.attribs + (data.scope === 'static' ? longname : name.startsWith('module:') ? name.split('/').pop() : name) + typeSignature ?>
<?js= this.partial('stability.tmpl', data) ?> <?js= this.partial('stability.tmpl', data) ?>
</h4> </h4>
</div> </div>

View File

@@ -2,7 +2,7 @@
var self = this; var self = this;
function toShortName(name) { function toShortName(name) {
return name.indexOf('module:') === 0 ? name.split('/').pop() : name; return name.startsWith('module:') ? name.split('/').pop() : name;
} }
function getItemCssClass(type) { function getItemCssClass(type) {

View File

@@ -1,11 +1,11 @@
<?js <?js
var params = obj; var params = obj;
/* sort subparams under their parent params (like opts.classname) */ /* sort subparams under their parent params (like opts.classname) */
var parentParam = null; var parentParam = null;
params.forEach(function(param, i) { params.forEach(function(param, i) {
if (!param) { return; } if (!param) { return; }
if ( parentParam && param.name && param.name.indexOf(parentParam.name + '.') === 0 ) { if (parentParam && param.name && param.name.startsWith(parentParam.name + '.')) {
param.name = param.name.substr(parentParam.name.length+1); param.name = param.name.substr(parentParam.name.length+1);
parentParam.subparams = parentParam.subparams || []; parentParam.subparams = parentParam.subparams || [];
parentParam.subparams.push(param); parentParam.subparams.push(param);
@@ -15,30 +15,30 @@
parentParam = param; parentParam = param;
} }
}); });
/* determine if we need extra "attributes" column */ /* determine if we need extra "attributes" column */
params.hasAttributes = false; params.hasAttributes = false;
params.hasName = false; params.hasName = false;
var colspan = 2; var colspan = 2;
params.forEach(function(param) { params.forEach(function(param) {
if (!param) { return; } if (!param) { return; }
if (param.type && param.type.names && param.type.names.includes('undefined')) { if (param.type && param.type.names && param.type.names.includes('undefined')) {
param.optional = true; param.optional = true;
} }
if (param.name.indexOf('var_') == 0) { if (param.name.startsWith('var_')) {
params.hasAttributes = true; params.hasAttributes = true;
param.variable = true; param.variable = true;
} }
if (param.optional || param.nullable) { if (param.optional || param.nullable) {
params.hasAttributes = true; params.hasAttributes = true;
} }
if (param.name) { if (param.name) {
params.hasName = true; params.hasName = true;
} }
}); });
?> ?>
@@ -48,25 +48,25 @@
<?js if (params.hasName) {?> <?js if (params.hasName) {?>
<th>Name</th> <th>Name</th>
<?js } ?> <?js } ?>
<th>Type</th> <th>Type</th>
<th class="last">Description</th> <th class="last">Description</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?js <?js
var self = this; var self = this;
params.forEach(function(param) { params.forEach(function(param) {
if (!param) { return; } if (!param) { return; }
?> ?>
<tr class="<?js= (param.stability && param.stability !== 'stable') ? 'unstable' : '' ?>"> <tr class="<?js= (param.stability && param.stability !== 'stable') ? 'unstable' : '' ?>">
<?js if (params.hasName) {?> <?js if (params.hasName) {?>
<td class="name"><code><?js= param.name.replace(/^opt_/, "") ?></code></td> <td class="name"><code><?js= param.name.replace(/^opt_/, "") ?></code></td>
<?js } ?> <?js } ?>
<?js if (!param.subparams) {?> <?js if (!param.subparams) {?>
<td class="type"> <td class="type">
<?js if (param.type && param.type.names) {?> <?js if (param.type && param.type.names) {?>
@@ -87,7 +87,7 @@
<?js= self.partial('params.tmpl', param.subparams) ?> <?js= self.partial('params.tmpl', param.subparams) ?>
<?js } ?></td> <?js } ?></td>
</tr> </tr>
<?js }); ?> <?js }); ?>
</tbody> </tbody>
</table> </table>

View File

@@ -1,11 +1,11 @@
<?js <?js
var props = obj; var props = obj;
/* sort subprops under their parent props (like opts.classname) */ /* sort subprops under their parent props (like opts.classname) */
var parentProp = null; var parentProp = null;
props.forEach(function(prop, i) { props.forEach(function(prop, i) {
if (!prop) { return; } if (!prop) { return; }
if ( parentProp && prop.name && prop.name.indexOf(parentProp.name + '.') === 0 ) { if (parentProp && prop.name && prop.name.startsWith(parentProp.name + '.')) {
prop.name = prop.name.substr(parentProp.name.length+1); prop.name = prop.name.substr(parentProp.name.length+1);
parentProp.subprops = parentProp.subprops || []; parentProp.subprops = parentProp.subprops || [];
parentProp.subprops.push(prop); parentProp.subprops.push(prop);
@@ -15,23 +15,23 @@
parentProp = prop; parentProp = prop;
} }
}); });
/* determine if we need extra columns, "attributes" and "default" */ /* determine if we need extra columns, "attributes" and "default" */
props.hasAttributes = false; props.hasAttributes = false;
props.hasDefault = false; props.hasDefault = false;
props.hasName = false; props.hasName = false;
props.forEach(function(prop) { props.forEach(function(prop) {
if (!prop) { return; } if (!prop) { return; }
if (prop.optional || prop.nullable) { if (prop.optional || prop.nullable) {
props.hasAttributes = true; props.hasAttributes = true;
} }
if (prop.name) { if (prop.name) {
props.hasName = true; props.hasName = true;
} }
if (typeof prop.defaultvalue !== 'undefined') { if (typeof prop.defaultvalue !== 'undefined') {
props.hasDefault = true; props.hasDefault = true;
} }
@@ -44,51 +44,51 @@
<?js if (props.hasName) {?> <?js if (props.hasName) {?>
<th>Name</th> <th>Name</th>
<?js } ?> <?js } ?>
<th>Type</th> <th>Type</th>
<?js if (props.hasAttributes) {?> <?js if (props.hasAttributes) {?>
<th>Argument</th> <th>Argument</th>
<?js } ?> <?js } ?>
<?js if (props.hasDefault) {?> <?js if (props.hasDefault) {?>
<th>Default</th> <th>Default</th>
<?js } ?> <?js } ?>
<th class="last">Description</th> <th class="last">Description</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?js <?js
var self = this; var self = this;
props.forEach(function(prop) { props.forEach(function(prop) {
if (!prop) { return; } if (!prop) { return; }
?> ?>
<tr> <tr>
<?js if (props.hasName) {?> <?js if (props.hasName) {?>
<td class="name"><code><?js= prop.name ?></code></td> <td class="name"><code><?js= prop.name ?></code></td>
<?js } ?> <?js } ?>
<td class="type"> <td class="type">
<?js if (prop.type && prop.type.names) {?> <?js if (prop.type && prop.type.names) {?>
<?js= self.partial('type.tmpl', prop.type.names) ?> <?js= self.partial('type.tmpl', prop.type.names) ?>
<?js } ?> <?js } ?>
</td> </td>
<?js if (props.hasAttributes) {?> <?js if (props.hasAttributes) {?>
<td class="attributes"> <td class="attributes">
<?js if (prop.optional) { ?> <?js if (prop.optional) { ?>
&lt;optional><br> &lt;optional><br>
<?js } ?> <?js } ?>
<?js if (prop.nullable) { ?> <?js if (prop.nullable) { ?>
&lt;nullable><br> &lt;nullable><br>
<?js } ?> <?js } ?>
</td> </td>
<?js } ?> <?js } ?>
<?js if (props.hasDefault) {?> <?js if (props.hasDefault) {?>
<td class="default"> <td class="default">
<?js if (typeof prop.defaultvalue !== 'undefined') { ?> <?js if (typeof prop.defaultvalue !== 'undefined') { ?>
@@ -96,12 +96,12 @@
<?js } ?> <?js } ?>
</td> </td>
<?js } ?> <?js } ?>
<td class="description last"><?js= prop.description ?><?js if (prop.subprops) { ?> <td class="description last"><?js= prop.description ?><?js if (prop.subprops) { ?>
<h6>Properties</h6><?js= self.partial('properties.tmpl', prop.subprops) ?> <h6>Properties</h6><?js= self.partial('properties.tmpl', prop.subprops) ?>
<?js } ?></td> <?js } ?></td>
</tr> </tr>
<?js }); ?> <?js }); ?>
</tbody> </tbody>
</table> </table>

View File

@@ -10,9 +10,9 @@ returns.forEach(function (ret, i) {
var name = ret.name || ret.description; var name = ret.name || ret.description;
var startSpacePos = name.indexOf(" "); var startSpacePos = name.indexOf(" ");
if (parentReturn !== null && name.indexOf(parentReturn.name + '.') === 0) { if (parentReturn !== null && name.startsWith(parentReturn.name + '.')) {
ret.name = isNamed ? name.substr(parentReturn.name.length + 1) : name.substr(parentReturn.name.length + 1, startSpacePos - (parentReturn.name.length + 1)); ret.name = isNamed ? name.substr(parentReturn.name.length + 1) : name.substr(parentReturn.name.length + 1, startSpacePos - (parentReturn.name.length + 1));
parentReturn.subReturns = parentReturn.subReturns || []; parentReturn.subReturns = parentReturn.subReturns || [];
parentReturn.subReturns.push(ret); parentReturn.subReturns.push(ret);
returns[i] = null; returns[i] = null;
@@ -20,7 +20,7 @@ returns.forEach(function (ret, i) {
if (!isNamed) { if (!isNamed) {
ret.name = ret.description.substr(0, startSpacePos !== -1 ? startSpacePos : ret.description.length); ret.name = ret.description.substr(0, startSpacePos !== -1 ? startSpacePos : ret.description.length);
} }
parentReturn = ret; parentReturn = ret;
} }
} }
@@ -54,7 +54,7 @@ if (returns.length > 1) {
ret.type.names.forEach(function(name, i) { ?> ret.type.names.forEach(function(name, i) { ?>
<?js= self.linkto(name, self.htmlsafe(name)) ?> <?js= self.linkto(name, self.htmlsafe(name)) ?>
<?js if (i < ret.type.names.length-1) { ?> | <?js } ?> <?js if (i < ret.type.names.length-1) { ?> | <?js } ?>
<?js }); <?js });
} }
?> ?>
</td> </td>

View File

@@ -34,7 +34,7 @@ exports.publish = function (data, opts) {
return ( return (
this.meta && this.meta &&
this.meta.path && this.meta.path &&
this.longname.indexOf('<anonymous>') !== 0 && !this.longname.startsWith('<anonymous>') &&
this.longname !== 'module:ol' this.longname !== 'module:ol'
); );
}, },

View File

@@ -29,10 +29,10 @@ exports.handlers = {
observable.name = name; observable.name = name;
observable.readonly = observable.readonly =
typeof observable.readonly == 'boolean' ? observable.readonly : true; typeof observable.readonly == 'boolean' ? observable.readonly : true;
if (doclet.name.indexOf('get') === 0) { if (doclet.name.startsWith('get')) {
observable.type = doclet.returns[0].type; observable.type = doclet.returns[0].type;
observable.description = doclet.returns[0].description; observable.description = doclet.returns[0].description;
} else if (doclet.name.indexOf('set') === 0) { } else if (doclet.name.startsWith('set')) {
observable.readonly = false; observable.readonly = false;
} }
if (doclet.stability) { if (doclet.stability) {

View File

@@ -27,7 +27,7 @@ function getKMLImage(href) {
let url = href; let url = href;
let path = window.location.href; let path = window.location.href;
path = path.slice(0, path.lastIndexOf('/') + 1); path = path.slice(0, path.lastIndexOf('/') + 1);
if (href.indexOf(path) === 0) { if (href.startsWith(path)) {
const regexp = new RegExp(href.replace(path, '') + '$', 'i'); const regexp = new RegExp(href.replace(path, '') + '$', 'i');
const kmlFile = zip.file(regexp)[0]; const kmlFile = zip.file(regexp)[0];
if (kmlFile) { if (kmlFile) {

View File

@@ -86,7 +86,7 @@ map.on(['click', 'pointermove'], function (event) {
} }
const fid = feature.getId(); const fid = feature.getId();
if (selectElement.value.indexOf('singleselect') === 0) { if (selectElement.value.startsWith('singleselect')) {
selection = {}; selection = {};
} }
// add selected feature to lookup // add selected feature to lookup

View File

@@ -168,11 +168,11 @@ function fromStringInternal_(s) {
} }
} }
color = [r, g, b, a / 255]; color = [r, g, b, a / 255];
} else if (s.indexOf('rgba(') == 0) { } else if (s.startsWith('rgba(')) {
// rgba() // rgba()
color = s.slice(5, -1).split(',').map(Number); color = s.slice(5, -1).split(',').map(Number);
normalize(color); normalize(color);
} else if (s.indexOf('rgb(') == 0) { } else if (s.startsWith('rgb(')) {
// rgb() // rgb()
color = s.slice(4, -1).split(',').map(Number); color = s.slice(4, -1).split(',').map(Number);
color.push(1); color.push(1);
@@ -225,7 +225,5 @@ export function isStringColor(s) {
if (NAMED_COLOR_RE_.test(s)) { if (NAMED_COLOR_RE_.test(s)) {
s = fromNamed(s); s = fromNamed(s);
} }
return ( return HEX_COLOR_RE_.test(s) || s.startsWith('rgba(') || s.startsWith('rgb(');
HEX_COLOR_RE_.test(s) || s.indexOf('rgba(') === 0 || s.indexOf('rgb(') === 0
);
} }

View File

@@ -822,7 +822,7 @@ function getTypeName(featurePrefix, featureType) {
featurePrefix = featurePrefix ? featurePrefix : FEATURE_PREFIX; featurePrefix = featurePrefix ? featurePrefix : FEATURE_PREFIX;
const prefix = featurePrefix + ':'; const prefix = featurePrefix + ':';
// The featureType already contains the prefix. // The featureType already contains the prefix.
if (featureType.indexOf(prefix) === 0) { if (featureType.startsWith(prefix)) {
return featureType; return featureType;
} else { } else {
return prefix + featureType; return prefix + featureType;

View File

@@ -864,7 +864,7 @@ class WKB extends FeatureFormat {
options.dataProjection && getProjection(options.dataProjection); options.dataProjection && getProjection(options.dataProjection);
if (dataProjection) { if (dataProjection) {
const code = dataProjection.getCode(); const code = dataProjection.getCode();
if (code.indexOf('EPSG:') === 0) { if (code.startsWith('EPSG:')) {
srid = Number(code.substring(5)); srid = Number(code.substring(5));
} }
} }

View File

@@ -114,7 +114,7 @@ export function getMapTileUrlTemplate(links, mediaType) {
} }
if (knownMapMediaTypes[link.type]) { if (knownMapMediaTypes[link.type]) {
fallbackUrlTemplate = link.href; fallbackUrlTemplate = link.href;
} else if (!fallbackUrlTemplate && link.type.indexOf('image/') === 0) { } else if (!fallbackUrlTemplate && link.type.startsWith('image/')) {
fallbackUrlTemplate = link.href; fallbackUrlTemplate = link.href;
} }
} }

View File

@@ -5,7 +5,7 @@ import {fileURLToPath} from 'url';
import {spawn} from 'child_process'; import {spawn} from 'child_process';
import {walk} from 'walk'; import {walk} from 'walk';
const isWindows = process.platform.indexOf('win') === 0; const isWindows = process.platform.startsWith('win');
const baseDir = dirname(fileURLToPath(import.meta.url)); const baseDir = dirname(fileURLToPath(import.meta.url));
const sourceDir = path.join(baseDir, '..', 'src'); const sourceDir = path.join(baseDir, '..', 'src');