Skip to content

Commit

Permalink
Add .format function
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Jan 28, 2018
1 parent f46bdc7 commit 5cf01ff
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 21 deletions.
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ const themingLog = require('theming-log');
const ansiColors = require('ansi-colors'); // Use ansi-colors for coloring in this example.

const emojiCry = String.fromCodePoint(0x1f622);
const emojiAstonish = String.fromCodePoint(0x1f632);

const themes = {
ERROR: 'red',
WARNING: msg => ansiColors.yellow(msg) + ' ' + emojiAstonish,

ERROR: 'red',
red: ansiColors.red,
yellow: ansiColors.yellow,

emoji: {
Cry: () => emojiCry,
}
Expand All @@ -38,8 +33,8 @@ const log = themingLog(themes);
log('{emoji.Cry} This is {ERROR: an error message: {1: error code} }.', 'E001');
// => '😢 This is \u001b[31man error message: E001\u001b[39m.'

log('This is {WARNING: a warning message.');
// => 'This is \u001b[33ma warning message\u001b[39m 😲'
var str = themingLog.format('{emoji.Cry} This is {ERROR: an error message: {1: error code} }.', 'E001');
// str === '😢 This is \u001b[31man error message: E001\u001b[39m.'
```

For Web browsers:
Expand All @@ -53,13 +48,8 @@ function setMessage(msg) {
const themes = {
ERROR: 'red',
WARNING: msg => '<span style="color:yellow">' + msg + '</span>&nbsp;' + String.fromCodePoint(0x1f632),
red: msg => '<span style="color:red">' + msg + '</span>',
yellow: msg => '<span style="color:yellow">' + msg + '</span>',
Cry: () => String.fromCodePoint(0x1f622),
Astonish: () => String.fromCodePoint(0x1f632),
};
const log = themingLog(themes, setMessage);
Expand All @@ -68,8 +58,8 @@ window.addEventListener('load', () => {
log('{Cry} This is {ERROR: an error message: {1: error code} }.', 'E001');
// => '😢 This is <span style="color:red">an error message: E001</span>.'
log('This is {WARNING: a warning message }');
// => 'This is <span style="color:yellow">a warning message</span> 😲'
var str = themingLog.format('{emoji.Cry} This is {ERROR: an error message: {1: error code} }.', 'E001');
// str === '😢 This is <span style="color:red">an error message: E001</span>.'
});
</script>
```
Expand Down Expand Up @@ -129,6 +119,25 @@ A logging function with theme for text decorations.
**Type:** function


### <u>.format(themes, fmt [, ...args]) : string</u>

Returns a formatted string from *fmt* with *themes* and *args*.

#### Parameters:

| Parameter | Type | Description |
|:------------|:--------:|:-------------------------------------------------------|
| *themes* | object | An object which has a set of theming text decorations. |
| *fmt* | string | a themed text (explained above) |
| *args* | *any* | themes for arguments (explained above) |

#### Returns:

A formatted string.

**Type:** string


## License

Copyright (C) 2018 Takayuki Sato.
Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
var parseThemedText = require('./lib/parse-themed-text');
var applyTheme = require('./lib/apply-theme');

function themingLog(theme, logger /* , ...args */) {
function themingLog(theme, logger) {
logger = logger || console.log;

return function(text) {
return function(text /*, ...args */) {
logger(applyTheme(parseThemedText(text), theme, arguments));
};
}

function format(theme, text /* , ...args */) {
var args = Array.prototype.slice.call(arguments, 1);
return applyTheme(parseThemedText(text), theme, args);
}

Object.defineProperty(themingLog, 'format', {
enumerable: true,
value: format,
});

module.exports = themingLog;
51 changes: 51 additions & 0 deletions test/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

/* eslint brace-style: off */

var chai = require('chai');
var expect = chai.expect;
var themingLog = require('..');

var format = themingLog.format;

describe('format', function() {

describe('Format text with decorations by themes', function() {

var themes = {
red: function(v) { return 'Red:[' + v + ']'; },
blue: function(v) { return 'Blue:[' + v + ']'; },
yellow: function(v) { return 'Yellow:[' + v + ']'; },
bold: function(v) { return 'Bold:[' + v + ']'; },
italic: function(v) { return 'Italic:[' + v + ']'; },
grinning: function() { return ':smile:'; },

INFO: null,
WARNING: 'yellow',
ERROR: 'red',
HIGHLIGHT: 'bold',

TypeError: 'error',
AppError: function(v) { return 'Magenta:[' + v + ']'; },
};

it('Should format no themed text', function() {
var out = format(themes, 'This text contains no theme');
expect(out).to.equal('This text contains no theme');
});

it('Should format text using theme', function() {
var out = format(themes,
'This {bold: text} contains {red: themed { italic : message }}.');
expect(out).to.equal('This Bold:[text] contains Red:[themed ' +
'Italic:[message]].');
});

it('Should replace arg-themes to argument values', function() {
var out = format(themes,
'This text has arg-theme: {2} and {1: One}', 'Arg1', 'Arg2');
expect(out).to.equal('This text has arg-theme: Arg2 and Arg1');
});
});

});
File renamed without changes.
54 changes: 54 additions & 0 deletions test/web/browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,60 @@
var expect = chai.expect;


var format = themingLog.format;

describe('format', function() {

describe('Format text with decorations by themes', function() {

var themes = {
red: function(v) { return 'Red:[' + v + ']'; },
blue: function(v) { return 'Blue:[' + v + ']'; },
yellow: function(v) { return 'Yellow:[' + v + ']'; },
bold: function(v) { return 'Bold:[' + v + ']'; },
italic: function(v) { return 'Italic:[' + v + ']'; },
grinning: function() { return ':smile:'; },

INFO: null,
WARNING: 'yellow',
ERROR: 'red',
HIGHLIGHT: 'bold',

TypeError: 'error',
AppError: function(v) { return 'Magenta:[' + v + ']'; },
};

it('Should format no themed text', function() {
var out = format(themes, 'This text contains no theme');
expect(out).to.equal('This text contains no theme');
});

it('Should format text using theme', function() {
var out = format(themes,
'This {bold: text} contains {red: themed { italic : message }}.');
expect(out).to.equal('This Bold:[text] contains Red:[themed ' +
'Italic:[message]].');
});

it('Should replace arg-themes to argument values', function() {
var out = format(themes,
'This text has arg-theme: {2} and {1: One}', 'Arg1', 'Arg2');
expect(out).to.equal('This text has arg-theme: Arg2 and Arg1');
});
});

});

})();
(function(){
'use strict';

/* eslint brace-style: off */


var expect = chai.expect;



describe('theming-log', function() {

Expand Down
14 changes: 12 additions & 2 deletions web/theming-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
var parseThemedText = require('./lib/parse-themed-text');
var applyTheme = require('./lib/apply-theme');

function themingLog(theme, logger /* , ...args */) {
function themingLog(theme, logger) {
logger = logger || console.log;

return function(text) {
return function(text /*, ...args */) {
logger(applyTheme(parseThemedText(text), theme, arguments));
};
}

function format(theme, text /* , ...args */) {
var args = Array.prototype.slice.call(arguments, 1);
return applyTheme(parseThemedText(text), theme, args);
}

Object.defineProperty(themingLog, 'format', {
enumerable: true,
value: format,
});

module.exports = themingLog;

},{"./lib/apply-theme":2,"./lib/parse-themed-text":4}],2:[function(require,module,exports){
Expand Down
2 changes: 1 addition & 1 deletion web/theming-log.min.js

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

Loading

0 comments on commit 5cf01ff

Please sign in to comment.