Conversation
| 0) test_with_version 8 ;; | ||
| 1) test_with_version 10 ;; | ||
| 2) npm install && npm test ;; | ||
| 0) test_with_version 10 ;; | ||
| 1) npm install && npm test ;; | ||
| 2) test_with_version 14 ;; |
There was a problem hiding this comment.
In order to use named capturing groups (in the test suite) I had to drop 8 from the set of Node.js versions.
| //. > const replaceString = S.curry3 ((what, replacement, string) => | ||
| //. . string.replace (what, replacement) | ||
| //. . ) | ||
| //. > const defineProperty = S.curry3 (Object.defineProperty) |
There was a problem hiding this comment.
It is not easy to find a built-in function that takes three arguments!
There was a problem hiding this comment.
You could use a variadic function, such as Math.max, right? Then you could create curried built-ins for any number of arguments.
There was a problem hiding this comment.
That's true. Binary versions of variadic functions are compelling, but the use cases for ternary versions are less apparent. I am happy to have found Object.defineProperty for this example. :)
|
Regarding naming, maybe we can use replace :: RegExp -> (Array (Maybe String) -> String) -> String -> String
replaceWith :: RegExp -> String -> String -> String(or @davidchambers said:
|
I did not realise that in the replacement string |
|
|
|
I like the idea of a function |
|
Closing in favour of #686 |
This pull request adds the following two functions:
Design decisions:
Both functions take
pattern :: RegExp, so there is no straightforward equivalent ofs1.replace (s2, s3)for stringss1,s2, ands3. One could useS.replace (S.regex ('') (S.regexEscape (s2))) (s3) (s1), but it would be simpler and clearer to useString#replacedirectly in such cases.Using
pattern :: RegExpexclusively provides two benefits:Simplicity. Supporting both
RegExpandStringwould require splittingreplaceinto two functions (which would pose a naming challenge) or usingEither RegExp String(which would not be ergonomic).Clarity. It's unclear whether
R.replace ('o') ('x') ('foo')should evaluate to'fxo'or to'fxx', whereas the presence of thegflag in/o/gindicates that all occurrences ofoshould be replaced, and the absence of thegflag in/o/indicates that only the first occurrence ofoshould be replaced.Like
replace',S.replace_ignores the “offset” and “string” arguments provided byString#replace(when given a function). These are rarely useful.Unlike
replace',S.replace_ignores the “groups” argument provided byString#replace(when given a function) if the pattern contains any named capturing groups. I included a fix for this deficiency in purescript/purescript-strings#126.Unlike
replace',S.replace_ignores the “match” argument provided byString#replace(when given a function). This simplifies the function's type, and saves one from usingS.K (...)when only the captured groups are important (as is commonly the case). If one does require access to the matched substring as a whole, one can use/(...)/to capture it. Admittedly, this approach is impractical if the RegExp object is defined in another module, but in such exceptional cases one can of course useString#replacedirectly.Unlike
replace',S.replace_acknowledges the existence of optional capturing groups:Evaluating the equivalent PureScript expression results in an exception being thrown:
I have proposed the use of
Array (Maybe String)in place ofArray Stringin purescript/purescript-strings#126.I have opened sanctuary-js/sanctuary-site#87 to provide several examples of
replaceandreplace_in use.@sanctuary-js/owners, what do you think of the proposed additions to the library?
@CrossEye, you may like to rethink
R.replace, which currently exposes all the quirks ofString#replace.