Skip to content

Commit

Permalink
improve toString + pprint function - #16 #17
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed May 30, 2019
1 parent 6ebc898 commit 20ae498
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
### Features
* `this` inside lambdas that are object created with new (same as in JS) [#19](https://github.com/jcubic/lips/issues/19)
* `string` and `type` return `instance` for objects created by `new`
* `pprint` function [#17](https://github.com/jcubic/lips/issues/17)
### Bug fixes
* fix set-obj! on functions [#18](https://github.com/jcubic/lips/issues/18)
* fix string formatting [#16](https://github.com/jcubic/lips/issues/16)

## 0.15.4
### Bug fixes
Expand Down
18 changes: 14 additions & 4 deletions dist/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Copyright (c) 2014-present, Facebook, Inc.
* released under MIT license
*
* build: Thu, 30 May 2019 19:39:55 +0000
* build: Thu, 30 May 2019 20:06:21 +0000
*/
(function () {
'use strict';
Expand Down Expand Up @@ -3964,7 +3964,8 @@
var obj_type = _typeof_1(obj);

if (isNull(obj) || obj_type !== 'object' && obj_type !== 'function') {
throw new Error(typeErrorMessage('set-obj!', type(obj), ['object', 'function']));
var msg = typeErrorMessage('set-obj!', type(obj), ['object', 'function']);
throw new Error(msg);
}

obj[key] = value;
Expand Down Expand Up @@ -4520,7 +4521,7 @@
}

if (obj instanceof Pair) {
return new lips.Formatter(obj.toString())["break"]().format();
return obj.toString();
}

if (obj instanceof _Symbol) {
Expand Down Expand Up @@ -4685,6 +4686,15 @@
});
}, "(read [string])\n\n Function if used with string will parse the string and return\n list structure of LIPS code. If called without an argument it\n will read string from standard input (using browser prompt or\n user defined way) and call itself with that string (parse is)\n function can be used together with eval to evaluate code from\n string"),
// ------------------------------------------------------------------
pprint: doc(function (arg) {
if (arg instanceof Pair) {
arg = new lips.Formatter(arg.toString())["break"]().format();
this.get('stdout').write(arg);
} else {
this.get('print').call(this, arg);
}
}, "(pprint expression)\n\n Pretty print list expression, if called with non-pair it just call\n print function with passed argument."),
// ------------------------------------------------------------------
print: doc(function () {
var _this$get,
_this3 = this;
Expand All @@ -4694,7 +4704,7 @@
}

(_this$get = this.get('stdout')).write.apply(_this$get, toConsumableArray(args.map(function (arg) {
return _this3.get('string')(arg);
return _this3.get('string')(arg, typeof arg === 'string');
})));
}, "(print . args)\n\n Function convert each argument to string and print the result to\n standard output (by default it's console but it can be defined\n it user code)"),
// ------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -2513,7 +2513,8 @@
'set-obj!': doc(function(obj, key, value) {
var obj_type = typeof obj;
if (isNull(obj) || (obj_type !== 'object' && obj_type !== 'function')) {
throw new Error(typeErrorMessage('set-obj!', type(obj), ['object', 'function']));
var msg = typeErrorMessage('set-obj!', type(obj), ['object', 'function']);
throw new Error(msg);
}
obj[key] = value;
}, `(set-obj! obj key value)
Expand Down Expand Up @@ -3047,7 +3048,7 @@
return JSON.stringify(obj).replace(/\\n/g, '\n');
}
if (obj instanceof Pair) {
return new lips.Formatter(obj.toString()).break().format();
return obj.toString();
}
if (obj instanceof Symbol) {
return obj.toString();
Expand Down Expand Up @@ -3249,9 +3250,21 @@
function can be used together with eval to evaluate code from
string`),
// ------------------------------------------------------------------
pprint: doc(function(arg) {
if (arg instanceof Pair) {
arg = new lips.Formatter(arg.toString()).break().format();
this.get('stdout').write(arg);
} else {
this.get('print').call(this, arg);
}
}, `(pprint expression)
Pretty print list expression, if called with non-pair it just call
print function with passed argument.`),
// ------------------------------------------------------------------
print: doc(function(...args) {
this.get('stdout').write(...args.map((arg) => {
return this.get('string')(arg);
return this.get('string')(arg, typeof arg === 'string');
}));
}, `(print . args)
Expand Down

0 comments on commit 20ae498

Please sign in to comment.