Files
30-seconds-of-code/snippets/left-substr-generator.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

25 lines
628 B
Markdown

---
title: Left substring generator
tags: string,generator
cover: boutique-home-office-1
author: chalarangelo
firstSeen: 2022-07-24T05:00:00-04:00
---
Generates all left substrings of a given string.
- Use `String.prototype.length` to terminate early if the string is empty.
- Use a `for...in` loop and `String.prototype.slice()` to `yield` each substring of the given string, starting at the beginning.
```js
const leftSubstrGenerator = function* (str) {
if (!str.length) return;
for (let i in str) yield str.slice(0, i + 1);
};
```
```js
[...leftSubstrGenerator('hello')];
// [ 'h', 'he', 'hel', 'hell', 'hello' ]
```