-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
regexp: add S.replaceWith and S.replaceBy #686
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1731,12 +1731,12 @@ | |
//. Curries the given ternary function. | ||
//. | ||
//. ```javascript | ||
//. > const replaceString = S.curry3 ((what, replacement, string) => | ||
//. . string.replace (what, replacement) | ||
//. . ) | ||
//. > const defineProperty = S.curry3 (Object.defineProperty) | ||
//. | ||
//. > const o = defineProperty ({}) ('x') ({value: 42, writable: false}) | ||
//. | ||
//. > replaceString ('banana') ('orange') ('banana icecream') | ||
//. 'orange icecream' | ||
//. > o.x | ||
//. 42 | ||
//. ``` | ||
function curry3(f) { | ||
return function(x) { | ||
|
@@ -4548,6 +4548,83 @@ | |
impl: matchAll | ||
}; | ||
|
||
//# replaceWith :: String -> RegExp -> String -> String | ||
//. | ||
//. Replaces occurrences of the given pattern with the given replacement | ||
//. within the given string. Replaces all occurrences of the pattern if | ||
//. its `g` flag is set; just the first occurrence otherwise. | ||
//. | ||
//. See also [`replaceBy`](#replaceBy). | ||
//. | ||
//. ```javascript | ||
//. > S.replaceWith ('x') (/o/) ('foo') | ||
//. 'fxo' | ||
//. | ||
//. > S.replaceWith ('x') (/o/g) ('foo') | ||
//. 'fxx' | ||
//. ``` | ||
function replaceWith(replacement) { | ||
return function(pattern) { | ||
return function(text) { | ||
return text.replace (pattern, function() { return replacement; }); | ||
}; | ||
}; | ||
} | ||
_.replaceWith = { | ||
consts: {}, | ||
types: [$.String, $.RegExp, $.String, $.String], | ||
impl: replaceWith | ||
}; | ||
|
||
//# replaceBy :: (Array (Maybe String) -> String) -> RegExp -> String -> String | ||
//. | ||
//. Replaces occurrences of the given pattern within the given string | ||
//. in accordance with the given replacement function, which receives an | ||
//. array of captured values. Replaces all occurrences of the pattern if | ||
//. its `g` flag is set; just the first occurrence otherwise. | ||
//. | ||
//. See also [`replaceWith`](#replaceWith). | ||
//. | ||
//. ```javascript | ||
//. > S.replaceBy (([$1]) => S.maybe ('') (S.toUpper) ($1)) (/(\w)/) ('foo') | ||
//. 'Foo' | ||
//. | ||
//. > S.replaceBy (([$1]) => S.maybe ('') (S.toUpper) ($1)) (/(\w)/g) ('foo') | ||
//. 'FOO' | ||
//. | ||
//. > S.replaceBy (S.show) (/(foo)(bar)?/) ('<>') | ||
//. '<>' | ||
//. | ||
//. > S.replaceBy (S.show) (/(foo)(bar)?/) ('<foo>') | ||
//. '<[Just ("foo"), Nothing]>' | ||
//. | ||
//. > S.replaceBy (S.show) (/(foo)(bar)?/) ('<foobar>') | ||
//. '<[Just ("foo"), Just ("bar")]>' | ||
//. ``` | ||
function replaceBy(substitute) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered naming it just
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent points. I completely agree. :) In fact, I suggest we only provide this function (as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how I feel about the exclusion of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest we try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good to me. |
||
return function(pattern) { | ||
return function(text) { | ||
return text.replace (pattern, function() { | ||
var groups = []; | ||
var group, idx = 1; | ||
// eslint-disable-next-line no-plusplus | ||
while (typeof (group = arguments[idx++]) !== 'number') { | ||
groups.push (group == null ? Nothing : Just (group)); | ||
} | ||
return substitute (groups); | ||
}); | ||
}; | ||
}; | ||
} | ||
_.replaceBy = { | ||
consts: {}, | ||
types: [$.Fn ($.Array ($.Maybe ($.String))) ($.String), | ||
$.RegExp, | ||
$.String, | ||
$.String], | ||
impl: replaceBy | ||
}; | ||
|
||
//. ### String | ||
|
||
//# toUpper :: String -> String | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
{ | ||
"root": true, | ||
"extends": ["../node_modules/sanctuary-style/eslint-es6.json"], | ||
"parserOptions": {"ecmaVersion": 2018}, | ||
"env": {"node": true}, | ||
"globals": {"suite": false, "test": false}, | ||
"rules": { | ||
"comma-dangle": ["error", {"arrays": "always-multiline", "objects": "always-multiline", "functions": "never"}], | ||
"max-len": ["off"] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const S = require ('..'); | ||
|
||
const eq = require ('./internal/eq'); | ||
|
||
|
||
test ('replaceBy', () => { | ||
|
||
eq (S.show (S.replaceBy)) ('replaceBy :: (Array (Maybe String) -> String) -> RegExp -> String -> String'); | ||
|
||
eq (S.replaceBy (([$1]) => S.maybe ('') (S.toUpper) ($1)) (/(\w)/) ('foo')) ('Foo'); | ||
eq (S.replaceBy (([$1]) => S.maybe ('') (S.toUpper) ($1)) (/(\w)/g) ('foo')) ('FOO'); | ||
eq (S.replaceBy (S.show) (/(foo)(bar)?/) ('<>')) ('<>'); | ||
eq (S.replaceBy (S.show) (/(foo)(bar)?/) ('<foo>')) ('<[Just ("foo"), Nothing]>'); | ||
eq (S.replaceBy (S.show) (/(foo)(bar)?/) ('<foobar>')) ('<[Just ("foo"), Just ("bar")]>'); | ||
eq (S.replaceBy (S.show) (/@(?<username>[-\w]+)/) ('@sanctuary-js')) ('[Just ("sanctuary-js")]'); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const S = require ('..'); | ||
|
||
const eq = require ('./internal/eq'); | ||
|
||
|
||
test ('replaceWith', () => { | ||
|
||
eq (S.show (S.replaceWith)) ('replaceWith :: String -> RegExp -> String -> String'); | ||
|
||
eq (S.replaceWith ('x') (/o/) ('<bar>')) ('<bar>'); | ||
eq (S.replaceWith ('x') (/o/) ('<foo>')) ('<fxo>'); | ||
eq (S.replaceWith ('x') (/o/g) ('<bar>')) ('<bar>'); | ||
eq (S.replaceWith ('x') (/o/g) ('<foo>')) ('<fxx>'); | ||
eq (S.replaceWith ('$1') (/(o)/) ('<bar>')) ('<bar>'); | ||
eq (S.replaceWith ('$1') (/(o)/) ('<foo>')) ('<f$1o>'); | ||
eq (S.replaceWith ('$1') (/(o)/g) ('<bar>')) ('<bar>'); | ||
eq (S.replaceWith ('$1') (/(o)/g) ('<foo>')) ('<f$1$1>'); | ||
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider
B (replaceBy) (K)
as the implementation ofreplaceWith
?