DEV Community

Ahmed Musallam
Ahmed Musallam

Posted on

I Challenge YOU to Write a Better Parser!

So here is the deal, I wrote this really Stupid Sling URL Parser:

GitHub logo ahmed-musallam / stupid-sling-url-parser

A really stupid JS to parse Apache Sling URL

A Really stupid Apache Sling URL Parser

This is a stupid implementation to parse Apache Sling Url in JS. You should really use Sluri.

I built this for fun. It only supports Modern browsers (Sorry not sorry IE) and NodeJs 10+. Also, it is EXTREMELY small! ~0.8KB minified. Sluri is 6.5 KB minified but has a lot more functionality.

Usage

Get the minified version in /dist

const slingUrl = new SlingUrl('/a/b.s1.html/c/d');


console.log(slingUrl.resourcePath) // returns: "/a/b"
console.log(slingUrl.selectors)    // returns ["s1"]
console.log(slingUrl.extension)    // returns "html"
console.log(slingUrl.suffix)       // returns "/c/d"

// if any of the properties above does not exist, null is returned.
Enter fullscreen mode Exit fullscreen mode

Source

see /src/SlingUrl.js

Running tests

npm test



The whole source is in this file: https://github.com/ahmed-musallam/stupid-sling-url-parser/blob/master/src/SlingUrl.js

I'm setting a pretty low bar here, since I wrote this very quickly for the SOLE purpose of saving bytes.

Now, I'd like to see you try to write something better! It would be a good exercise if you're up for it. Plus you'd show some random person on the internet how wrong they are.

All you need to know to rebuild this is in the URL Decomposition Doc of Apache Sling

The doc has examples and I have written tests for my parser

Happy coding!

Top comments (0)