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>
<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) ?>
</h4>
</div>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -29,10 +29,10 @@ exports.handlers = {
observable.name = name;
observable.readonly =
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.description = doclet.returns[0].description;
} else if (doclet.name.indexOf('set') === 0) {
} else if (doclet.name.startsWith('set')) {
observable.readonly = false;
}
if (doclet.stability) {

View File

@@ -27,7 +27,7 @@ function getKMLImage(href) {
let url = href;
let path = window.location.href;
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 kmlFile = zip.file(regexp)[0];
if (kmlFile) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,7 +5,7 @@ import {fileURLToPath} from 'url';
import {spawn} from 'child_process';
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 sourceDir = path.join(baseDir, '..', 'src');