From c230bd56d07d2263ac89c8daa801505f1aecdf58 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Mon, 26 Mar 2018 13:44:32 +0200 Subject: [PATCH] readme: convert examples to ES6 --- README.md | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a0ee36e..10b2646 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ The equivalent concatenation: 2. Require: ```javascript - var format = require('string-format') + const format = require('string-format') ``` #### Browser @@ -122,8 +122,8 @@ A format string must not contain both implicit and explicit references: Dot notation may be used to reference object properties: ```javascript -var bobby = {firstName: 'Bobby', lastName: 'Fischer'} -var garry = {firstName: 'Garry', lastName: 'Kasparov'} +const bobby = {firstName: 'Bobby', lastName: 'Fischer'} +const garry = {firstName: 'Garry', lastName: 'Kasparov'} '{0.firstName} {0.lastName} vs. {1.firstName} {1.lastName}'.format(bobby, garry) // => 'Bobby Fischer vs. Garry Kasparov' @@ -132,7 +132,7 @@ var garry = {firstName: 'Garry', lastName: 'Kasparov'} `0.` may be omitted when referencing a property of `{0}`: ```javascript -var repo = {owner: 'davidchambers', slug: 'string-format'} +const repo = {owner: 'davidchambers', slug: 'string-format'} 'https://github.com/{owner}/{slug}'.format(repo) // => 'https://github.com/davidchambers/string-format' @@ -142,12 +142,12 @@ If the referenced property is a method, it is invoked with no arguments to determine the replacement: ```javascript -var sheldon = { +const sheldon = { firstName: 'Sheldon', lastName: 'Cooper', dob: new Date('1970-01-01'), - fullName: function() { return '{firstName} {lastName}'.format(this) }, - quip: function() { return 'Bazinga!' } + fullName: function() { return this.firstName + ' ' + this.lastName }, + quip: function() { return 'Bazinga!' }, } '{fullName} was born at precisely {dob.toISOString}'.format(sheldon) @@ -166,21 +166,14 @@ name in a template string. ```javascript format.extend(String.prototype, { - escape: function(s) { - return s.replace(/[&<>"'`]/g, function(c) { - return '&#' + c.charCodeAt(0) + ';' - }) - }, - upper: function(s) { return s.toUpperCase() } + escape: s => s.replace(/[&<>"'`]/g, c => '&#' + c.charCodeAt(0) + ';'), + upper: s => s.toUpperCase(), }) 'Hello, {!upper}!'.format('Alice') // => 'Hello, ALICE!' -var restaurant = { - name: 'Anchor & Hope', - url: 'http://anchorandhopesf.com/' -} +const restaurant = {name: 'Anchor & Hope', url: 'http://anchorandhopesf.com/'} '{name!escape}'.format(restaurant) // => 'Anchor & Hope'