Skip to content

Commit

Permalink
fix replace with async lambda #319
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Mar 5, 2024
1 parent 7ac3ef6 commit 25574d8
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* fix `do` macro [#324](https://github.com/jcubic/lips/issues/324)
* fix `string->number` that leads to NaN [#326](https://github.com/jcubic/lips/issues/326)
* fix unintentional unboxing in `iterator->array` [#328](https://github.com/jcubic/lips/issues/328)
* fix `replace` with async `lambda` [#319](https://github.com/jcubic/lips/issues/319)

## 1.0.0-beta.18
### Breaking
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![npm](https://img.shields.io/badge/npm-1.0.0%E2%80%93beta.18.1-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
![1.0.0 Complete](https://img.shields.io/github/milestones/progress-percent/jcubic/lips/1?label=1.0.0%20Complete)
[![Build and test](https://github.com/jcubic/lips/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/lips/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&a520631e7f12f3eda2816e5633437fc8)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&6d6875155398b050c8cd14e1bcc09224)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Join Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jcubic/lips)
![NPM Download Count](https://img.shields.io/npm/dm/@jcubic/lips)
![JSDelivr Download count](https://img.shields.io/jsdelivr/npm/hm/@jcubic/lips)
Expand Down Expand Up @@ -354,7 +354,8 @@ I would also love to see if you use the library, I may even share the links of p
* [StackOverlow](https://stackoverflow.com) code was used for functions:
* [fworker](https://stackoverflow.com/a/10372280/387194),
* [flatten](https://stackoverflow.com/a/27282907/387194),
* [allPossibleCases](https://stackoverflow.com/a/4331218/387194).
* [allPossibleCases](https://stackoverflow.com/a/4331218/387194),
* [async replace](https://stackoverflow.com/a/48032528/387194).
* Code formatter is roughly based on [scheme-style](http://community.schemewiki.org/?scheme-style) and GNU Emacs scheme mode.
* Some helpers in standard library are inspired by same functions from [RamdaJS library](https://ramdajs.com/).

Expand Down
20 changes: 16 additions & 4 deletions dist/lips.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions dist/lips.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.esm.min.js

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions dist/lips.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -9076,6 +9076,16 @@ var global_env = new Environment({
typecheck('replace', pattern, ['regex', 'string']);
typecheck('replace', replacement, ['string', 'function']);
typecheck('replace', string, 'string');
if (is_function(replacement)) {
// ref: https://stackoverflow.com/a/48032528/387194
const replacements = [];
string.replace(pattern, function(...args) {
replacements.push(replacement(...args));
});
return unpromise(replacements, replacements => {
return string.replace(pattern, () => replacements.shift());
});
}
return string.replace(pattern, replacement);
}, `(replace pattern replacement string)
Expand Down
3 changes: 2 additions & 1 deletion templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ I would also love to see if you use the library, I may even share the links of p
* [StackOverlow](https://stackoverflow.com) code was used for functions:
* [fworker](https://stackoverflow.com/a/10372280/387194),
* [flatten](https://stackoverflow.com/a/27282907/387194),
* [allPossibleCases](https://stackoverflow.com/a/4331218/387194).
* [allPossibleCases](https://stackoverflow.com/a/4331218/387194),
* [async replace](https://stackoverflow.com/a/48032528/387194).
* Code formatter is roughly based on [scheme-style](http://community.schemewiki.org/?scheme-style) and GNU Emacs scheme mode.
* Some helpers in standard library are inspired by same functions from [RamdaJS library](https://ramdajs.com/).

Expand Down
4 changes: 4 additions & 0 deletions tests/core.scm
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@
(t.is (number->string 1.0e+27 16) "3.3b2e3c9fd0804e+16")
(t.is (number->string 1000000000000000000000000000 16) "33b2e3c9fd0803ce8000000")))

(test "core: replace async"
(lambda (t)
(t.is (replace #/foo/ (lambda () (Promise.resolve "lips")) "foo bar") "lips bar")))

;; TODO
;; begin*
;; set-obj! throws with null or boolean
Expand Down

0 comments on commit 25574d8

Please sign in to comment.