-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Helpers for implementing improved frontmatter in helix-pipe
feat(string.js): New helper library for working with strings feat(sequence.js): dict() used for casting values to es6 maps feat(sequence.js): obj() used for casting values to k/v objects chore(sequence.js): Refactor trySkip to use range0(..) instead range(0, ...) feat(sequence.js): reject() opposite of filter() feat(sequence.js): take() throwing version of tryTake() feat(sequence.js): zipLeast2() curryable version of zipLeast() feat(sequence.js): zip2() curryable version of zip() feat(sequence.js): zipLongest2() curryable version of zipLongest() feat(sequence.js): slidingWindow() feat(sequence.js): trySlidingWindow() feat(sequence.js): lookahead() feat(sequence.js): mod() Transform value as a sequence then cast back to original type feat(sequence.js): union() Combine set or map-like collections and cast back to original type feat(sequence.js): union2() curryable version of union()
- Loading branch information
Showing
5 changed files
with
403 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2018 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
const { | ||
pipe, filter, map, count, foldl, join, size, | ||
} = require('./sequence.js'); | ||
|
||
/** | ||
* Helpers for working with strings. | ||
*/ | ||
|
||
/** | ||
* This is a helper for declaring multiline strings. | ||
* | ||
* ``` | ||
* const s = multiline(` | ||
* Foo | ||
* Bar | ||
* Baz | ||
* | ||
* Hello | ||
* | ||
* Bang | ||
* `); | ||
* ``` | ||
* | ||
* The function basically just takes a string and then | ||
* strips the first & last lines if they are empty. | ||
* | ||
* In order to remove indentation, we determine the common | ||
* whitespace prefix length (number of space 0x20 characters | ||
* at the start of the line). This prefix is simply removed | ||
* from each line... | ||
*/ | ||
const multiline = (str) => { | ||
// Discard the leading & trailing line | ||
const lines = str.split('\n'); | ||
|
||
// Strip the first and the last line | ||
if (lines[0].match(/^\s*$/)) { | ||
lines.shift(); | ||
} | ||
if (size(lines) > 0 && lines[size(lines) - 1].match(/^\s*$/)) { | ||
lines.pop(); | ||
} | ||
|
||
// Find the prefix length | ||
const prefixLen = pipe( | ||
lines, | ||
filter(l => !l.match(/^\s*$/)), // Disregarding empty lines | ||
map(l => l.match(/^ */)[0]), // Extract prefixes | ||
map(count), // calculate length | ||
foldl(Infinity, (a, b) => Math.min(a, b)), | ||
); // minimum | ||
|
||
return pipe( | ||
lines, | ||
map(l => l.slice(prefixLen)), // discard prefixes | ||
join('\n'), | ||
); | ||
}; | ||
|
||
module.exports = { multiline }; |
Oops, something went wrong.