Last active
March 26, 2016 02:47
-
-
Save craigdmckenna/24f92cfb4d8527b4c058 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tested on Node REPL v4.0.0 | |
"use strict"; | |
const tests = { | |
case1: { | |
name: 'Case 1', | |
string: 'Automotive', | |
expect: 'A6e' | |
}, | |
case2: { | |
name: 'Case 2', | |
string: 'Transforming the way the world buys, sells and owns cars.', | |
expect: 'T8g t1e w1y t1e w3d b2s, s2s a1d o2s c2s.' | |
}, | |
case3: { | |
name: 'Case 3', | |
string: 'Dealertrack is the leading provider of digital solutions to the automotive retail industry. ' + | |
'Whether you’re a dealer, a lender, a manufacturer, or a state agency, our integrated and ' + | |
'intuitive approach to products and services makes your workflows more efficient, transparent, ' + | |
'and profitable. From our pioneering Digital Retailing tools—bridging the gap between the ' + | |
'online and in-store experience—to our lender network (the largest in North America) ' + | |
'Dealertrack is the only company helping enable the transformation of auto retailing.', | |
expect: 'D6k i0s t1e l5g p6r o0f d4l s6s t0o t1e a6e r4l i6y. ' + | |
'W3r y1u’r0e a d3r, a l3r, a m8r, o0r a s2e a4y, o1r i6d a1d ' + | |
'i5e a5h t0o p6s a1d s5s m3s y2r w6s m2e e5t, t6t, ' + | |
'a1d p8e. F2m o1r p5g D4l R6g t2s—b5g t1e g1p b3n t1e ' + | |
'o3e a1d i0n-s3e e7e—t0o o1r l3r n5k (t1e l5t i0n N3h A5a) ' + | |
'D6k i0s t1e o2y c5y h5g e4e t1e t9n o0f a2o r6g.' | |
}, | |
case4: { | |
name: 'Case 4', | |
string: 'Lake Success, N.Y., March 23, 2016 – Helping to further advance the adoption of digital ' + | |
'technologies among automotive finance lenders and dealers, Dealertrack® today announced ' + | |
'that Ally Financial is now broadly available for eContracting through the Dealertrack platform.', | |
expect: 'L2e S4s, N.Y., M3h 23, 2016 – H5g t0o f5r a5e t1e a5n o0f d4l ' + | |
't8s a3g a6e f4e l4s a1d d4s, D6k® t3y a5d ' + | |
't2t A1y F4l i0s n1w b5y a5e f1r e8g t5h t1e D6k p6m.' | |
}, | |
case5: { | |
name: 'Case 5', | |
string: '----12svd3f%+=floo r~59nrtx.456yn', | |
expect: '----12s1d3f%+=f2o r~59n2x.456y0n' | |
} | |
}; | |
function testModifyString (testObj) { | |
let result = modifyString(testObj.string); | |
if (result === testObj.expect) { | |
console.log('(*^_^*) Test ' + testObj.name + ', Passing.'); | |
} else { | |
console.warn( | |
'[!] Test ' + testObj.name + ', Failing. Expected: "' + | |
result + '", to be: "' + testObj.expect + '"' | |
); | |
} | |
} | |
testModifyString(tests.case1); | |
testModifyString(tests.case2); | |
testModifyString(tests.case3); | |
testModifyString(tests.case4); | |
testModifyString(tests.case5); | |
function modifyString (string) { | |
const regexNonAlpha = /([^A-z])/; | |
const arr = string.split(regexNonAlpha).map((str) => { | |
let modifiedStr; | |
if (str.length >= 2) { | |
let uniqueCount = 0; | |
let middleChars = str.slice(1, -1).split('').sort(); | |
for (let i = 0; i < middleChars.length; i++) { | |
if (middleChars[i] !== middleChars[i - 1]) { | |
uniqueCount += 1; | |
} | |
} | |
modifiedStr = str[0] + uniqueCount + str[str.length - 1]; | |
} else { | |
modifiedStr = str; | |
} | |
return modifiedStr; | |
}); | |
return arr.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment