Skip to main content
replaced Array.from(array) with [...array]
Source Link
CPHPython
  • 13.9k
  • 5
  • 68
  • 77

I'm surprised no one used reduce, this is a native concise and powerful JavaScript function.

ES6 (EcmaScript2015)

String.prototype.format = function() {
  return Array[.from(arguments)..arguments].reduce((p,c) => p.replace(/%s/,c), this);
};

console.log('Is that a %s or a %s?... No, it\'s %s!'.format('plane', 'bird', 'SOman'));

< ES6

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

How it works:

reduce applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

var _r= function(p,c){return p.replace(/%s/,c)};

console.log(
  ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]") + '\n',
  [1, 2, 3].reduce(_r, "%s+%s=%s") + '\n',
  ["cool", 1337, "stuff"].reduce(_r, "%s %s %s")
);

I'm surprised no one used reduce, this is a native concise and powerful JavaScript function.

ES6 (EcmaScript2015)

String.prototype.format = function() {
  return Array.from(arguments).reduce((p,c) => p.replace(/%s/,c), this);
};

console.log('Is that a %s or a %s?... No, it\'s %s!'.format('plane', 'bird', 'SOman'));

< ES6

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

How it works:

reduce applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

var _r= function(p,c){return p.replace(/%s/,c)};

console.log(
  ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]") + '\n',
  [1, 2, 3].reduce(_r, "%s+%s=%s") + '\n',
  ["cool", 1337, "stuff"].reduce(_r, "%s %s %s")
);

I'm surprised no one used reduce, this is a native concise and powerful JavaScript function.

ES6 (EcmaScript2015)

String.prototype.format = function() {
  return [...arguments].reduce((p,c) => p.replace(/%s/,c), this);
};

console.log('Is that a %s or a %s?... No, it\'s %s!'.format('plane', 'bird', 'SOman'));

< ES6

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

How it works:

reduce applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

var _r= function(p,c){return p.replace(/%s/,c)};

console.log(
  ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]") + '\n',
  [1, 2, 3].reduce(_r, "%s+%s=%s") + '\n',
  ["cool", 1337, "stuff"].reduce(_r, "%s %s %s")
);

Added a prototype one-liner definition in ES6
Source Link
CPHPython
  • 13.9k
  • 5
  • 68
  • 77

I'm surprised no one used reducereduce, this is a native JavaScript way of doing things, is very concise and powerful JavaScript function.

var _r=function(p,c){return p.replace(/%s/,c);}

var x = ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]"); // [a], [b] and [c]

var y = [1, 2, 3].reduce(_r, "%s+%s=%s"); // 1+2=3

var z = ["cool", 1337, "stuff"].reduce(_r, "%s %s %s"); // cool 1337 stuff

ES6 (EcmaScript2015)

Edit: here is a function that you can insert anywhere to do replace.

String.prototype.format = function() {
  return Array.from(arguments).reduce((p,c) => p.replace(/%s/,c), this);
};

console.log('Is that a %s or a %s?... No, it\'s %s!'.format('plane', 'bird', 'SOman'));

< ES6

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

How it works:

reduce applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

var _r= function(p,c){return p.replace(/%s/,c)};

console.log(
  ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]") + '\n',
  [1, 2, 3].reduce(_r, "%s+%s=%s") + '\n',
  ["cool", 1337, "stuff"].reduce(_r, "%s %s %s")
);

I'm surprised no one used reduce, this is a native JavaScript way of doing things, is very concise and powerful.

var _r=function(p,c){return p.replace(/%s/,c);}

var x = ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]"); // [a], [b] and [c]

var y = [1, 2, 3].reduce(_r, "%s+%s=%s"); // 1+2=3

var z = ["cool", 1337, "stuff"].reduce(_r, "%s %s %s"); // cool 1337 stuff

Edit: here is a function that you can insert anywhere to do replace.

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

I'm surprised no one used reduce, this is a native concise and powerful JavaScript function.

ES6 (EcmaScript2015)

String.prototype.format = function() {
  return Array.from(arguments).reduce((p,c) => p.replace(/%s/,c), this);
};

console.log('Is that a %s or a %s?... No, it\'s %s!'.format('plane', 'bird', 'SOman'));

< ES6

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

How it works:

reduce applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

var _r= function(p,c){return p.replace(/%s/,c)};

console.log(
  ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]") + '\n',
  [1, 2, 3].reduce(_r, "%s+%s=%s") + '\n',
  ["cool", 1337, "stuff"].reduce(_r, "%s %s %s")
);

Changed the function name to "interpolate", to make it less confusing. Also added regex as a variable, to make it more clear what's happening.
Source Link
Monarch Wadia
  • 5.1k
  • 4
  • 40
  • 39

I'm surprised no one used reduce, this is a native JavaScript way of doing things, is very concise and powerful.

var _r=function(p,c){return p.replace(/%s/,c);}

var x = ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]"); // [a], [b] and [c]

var y = [1, 2, 3].reduce(_r, "%s+%s=%s"); // 1+2=3

var z = ["cool", 1337, "stuff"].reduce(_r, "%s %s %s"); // cool 1337 stuff

Edit: here is a function that you can insert anywhere to do replace.

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"

I'm surprised no one used reduce, this is a native JavaScript way of doing things, is very concise and powerful.

var _r=function(p,c){return p.replace(/%s/,c);}

var x = ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]"); // [a], [b] and [c]

var y = [1, 2, 3].reduce(_r, "%s+%s=%s"); // 1+2=3

var z = ["cool", 1337, "stuff"].reduce(_r, "%s %s %s"); // cool 1337 stuff

I'm surprised no one used reduce, this is a native JavaScript way of doing things, is very concise and powerful.

var _r=function(p,c){return p.replace(/%s/,c);}

var x = ["a", "b", "c"].reduce(_r, "[%s], [%s] and [%s]"); // [a], [b] and [c]

var y = [1, 2, 3].reduce(_r, "%s+%s=%s"); // 1+2=3

var z = ["cool", 1337, "stuff"].reduce(_r, "%s %s %s"); // cool 1337 stuff

Edit: here is a function that you can insert anywhere to do replace.

function interpolate(theString, argumentArray) {
    var regex = /%s/;
    var _r=function(p,c){return p.replace(regex,c);}
    return argumentArray.reduce(_r, theString);
}

interpolate("%s, %s and %s", ["Me", "myself", "I"]); // "Me, myself and I"
Source Link
Monarch Wadia
  • 5.1k
  • 4
  • 40
  • 39
Loading
Post Made Community Wiki by Monarch Wadia