Quick implementation of splitter algorithm with use of regular expressions.
Probably could be done better if offset characters would be cut off & added to result array rather than adding some random chars to _str and removing them right before returning result.
Also instead of using regexp straightforward for-loop can make it better - because of lack of positive lookbehind in js rexegp implementations results are not always OK (problem with strings which length is not divisible by _n).
function splitter (_str, _n) { _n = parseInt(_n); var offset = _n - _str.length % _n; if (offset !== _n) { _str += Math.pow(10, offset - 1); } var result = _str.split(new RegExp('(?=(?:.{' + _n + '})+$)', 'g')); if (offset !== _n) { var len = result.length, lastGroup = result[len - 1]; result[len - 1] = lastGroup.substring(0, lastGroup.length - offset); } return result; }
console.log('123456'.split(''));- function splitter (_str, _n) {
- _n = parseInt(_n);
- var offset = _n - _str.length % _n;
- if (offset !== _n) {
- _str += Math.pow(10, offset - 1);
- }
- var result = _str.split(new RegExp('(?=(?:.{' + _n + '})+$)', 'g'));
- if (offset !== _n) {
- var len = result.length,
- lastGroup = result[len - 1];
- result[len - 1] = lastGroup.substring(0, lastGroup.length - offset);
- }
- return result;
- }
var testString = '1234567890'; describe("Solution", function(){ it("should take number as second argument", function(){ Test.assertSimilar(splitter(testString, 2), ['12', '34', '56', '78', '90'], 'Split into 2-character group with use of numeric argument.'); Test.assertSimilar(splitter(testString, 3), ['123', '456', '789', '0'], 'Split into 3-character group with use of numeric argument.'); Test.assertSimilar(splitter(testString, 4), ['1234', '5678', '90'], 'Split into 4-character group with use of numeric argument.'); }); it("should take string as second argument", function(){ Test.assertSimilar(splitter(testString, '2'), ['12', '34', '56', '78', '90'], 'Split into 2-character group with use of string argument.'); Test.assertSimilar(splitter(testString, '3'), ['123', '456', '789', '0'], 'Split into 3-character group with use of string argument.'); Test.assertSimilar(splitter(testString, '4'), ['1234', '5678', '90'], 'Split into 4-character group with use of string argument.'); }); });
// TODO: Replace examples and use TDD development by writing your own tests// These are some CW specific test methods available:// Test.expect(boolean, [optional] message)// Test.assertEquals(actual, expected, [optional] message)// Test.assertSimilar(actual, expected, [optional] message)// Test.assertNotEquals(actual, expected, [optional] message)// NodeJS assert is also automatically required for you.// assert(true)// assert.strictEqual({a: 1}, {a: 1})// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})// You can also use Chai (http://chaijs.com/) by requiring it yourself// var expect = require("chai").expect;// var assert = require("chai").assert;// require("chai").should();- var testString = '1234567890';
- describe("Solution", function(){
it("should test for something", function(){Test.assertEquals("actual", "actual", "This is just an example of how you can write your own TDD tests");- it("should take number as second argument", function(){
- Test.assertSimilar(splitter(testString, 2), ['12', '34', '56', '78', '90'], 'Split into 2-character group with use of numeric argument.');
- Test.assertSimilar(splitter(testString, 3), ['123', '456', '789', '0'], 'Split into 3-character group with use of numeric argument.');
- Test.assertSimilar(splitter(testString, 4), ['1234', '5678', '90'], 'Split into 4-character group with use of numeric argument.');
- });
- it("should take string as second argument", function(){
- Test.assertSimilar(splitter(testString, '2'), ['12', '34', '56', '78', '90'], 'Split into 2-character group with use of string argument.');
- Test.assertSimilar(splitter(testString, '3'), ['123', '456', '789', '0'], 'Split into 3-character group with use of string argument.');
- Test.assertSimilar(splitter(testString, '4'), ['1234', '5678', '90'], 'Split into 4-character group with use of string argument.');
- });
- });