Created
January 11, 2017 20:37
-
-
Save neolitec/5965f84f0b7d47168b403f514a2b2b84 to your computer and use it in GitHub Desktop.
IPv6 abbreviation
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
function abbreviateIpv6(ip) { | |
var out = ip.replace(/(^|\:)0{1,3}([0-9]+)/g, "$1$2"); | |
out = out.replace(/0(\:0)+/, ":"); | |
out = out.replace(/\:{3,}/g, "::"); | |
if (out === ":") { | |
out = "::"; | |
} | |
return out; | |
} |
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
describe('To abbreviate IPv6 addresses.', function () { | |
it('should abbreviate IPv6 addresses', function () { | |
expect(abbreviateIpv6('2001:0000:0111:0000:0011:0000:0001:0000')).toBe('2001:0:111:0:11:0:1:0'); | |
expect(abbreviateIpv6('0000:0000:0111:0000:0011:0000:0001:0000')).toBe('::111:0:11:0:1:0'); | |
expect(abbreviateIpv6('2001:0000:0111:0000:0011:0001:0000:0000')).toBe('2001:0:111:0:11:1::'); | |
expect(abbreviateIpv6('2001:0000:0111:0000:0011:0000:0001:0000')).toBe('2001:0:111:0:11:0:1:0'); | |
expect(abbreviateIpv6('2001:0001:0000:0001:0000:0000:0000:0001')).toBe('2001:1:0:1::1'); | |
expect(abbreviateIpv6('2001:0001:0000:0001:0000:0000:0000:0000')).toBe('2001:1:0:1::'); | |
expect(abbreviateIpv6('0000:0000:0000:0000:0000:0000:0000:0000')).toBe('::'); | |
expect(abbreviateIpv6('0000:0000:0000:0000:0000:0000:0000:0001')).toBe('::1'); | |
expect(abbreviateIpv6('0000:0000:0000:0001:0000:0000:0000:0001')).toBe('::1:0:0:0:1'); | |
expect(abbreviateIpv6('0000:0000:0001:0000:0000:0001:0000:0000')).toBe('::1:0:0:1:0:0'); | |
expect(abbreviateIpv6('2041:0000:140F:0000:0000:0000:875B:131B')).toBe('2041:0:140F::875B:131B'); | |
expect(abbreviateIpv6('2001:0001:0002:0003:0004:0005:0006:0007')).toBe('2001:1:2:3:4:5:6:7'); | |
expect(abbreviateIpv6('2001:0000:0000:0000:1111:0000:0000:0000')).toBe('2001::1111:0:0:0'); | |
expect(abbreviateIpv6('2001:db8:0:0:0:0:2:1')).toBe('2001:db8::2:1'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment