Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Created September 1, 2021 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otobrglez/cc3487d9bacb14a93879f7d9feaad5dd to your computer and use it in GitHub Desktop.
Save otobrglez/cc3487d9bacb14a93879f7d9feaad5dd to your computer and use it in GitHub Desktop.
Poking around with regex
const assert = require('assert');
describe('should-work', () => {
it('passes', () => {
const enhanceURL = (url) =>
url.replace(/https/g, 'http').replace(/^http:\/\/([a-zA-Z0-9]+)\./gm,
(m, p1) => m.replace(p1, 'admin--' + p1))
.replace(/\/(?=[^\/]*$)/, '/api/')
+ '?_content_format=json'
assert.equal(
enhanceURL('http://example.ldp-project.localdev.space/channel-info'),
'http://admin--example.ldp-project.localdev.space/api/channel-info?_content_format=json'
)
assert.equal(
enhanceURL('https://example.ldp-project.localdev.space/channel-info'),
'http://admin--example.ldp-project.localdev.space/api/channel-info?_content_format=json'
)
assert.equal(
enhanceURL('https://oto.ldp-project.localdev.space/channel-info'),
'http://admin--oto.ldp-project.localdev.space/api/channel-info?_content_format=json'
)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment