Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,24 @@ Can also be required directly:
```js
import reflow from "@gustavnikolaj/string-utils/reflow";
```

## qw

A [qw (quote word) helper](https://perlmaven.com/qw-quote-word) for old,
disgruntled perl programmers.

```js
import { qw } from "@gustavnikolaj/string-utils";

const blah = "hey";

console.log(qw` foo bar
quux ${blah} baz
`); // => [ 'foo', 'bar', 'quux', 'hey', 'baz' ]
```

Can also be imported directly:

```js
import qw from "@gustavnikolaj/string-utils/qw";
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"index.js",
"collapse.js",
"deindent.js",
"reflow.js"
"reflow.js",
"qw.js"
],
"scripts": {
"build": "babel ./src --out-dir .",
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export const collapse = _collapse;

import _reflow from "./reflow";
export const reflow = _reflow;

import _qw from "./qw";
export const qw = _qw;
9 changes: 9 additions & 0 deletions src/qw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const zip = (strs, ...args) =>
strs.map((str, i) => (args[i] ? str + args[i] : str));

export default function qw(strs, ...args) {
return zip(strs, ...args)
.join("")
.split(/\s+/)
.filter(item => item);
}
17 changes: 17 additions & 0 deletions test/qw.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import expect from "unexpected";
import qw from "../src/qw";

describe("qw", () => {
it("should split a string into an array of words", () => {
const blah = "hey";

expect(
qw`
foo bar
quux ${blah} baz
`,
"to equal",
["foo", "bar", "quux", "hey", "baz"]
);
});
});