Skip to content

Instantly share code, notes, and snippets.

@menduz

menduz/QueryString.dwl

Last active Nov 11, 2017
Embed
What would you like to do?
TDD in DataWeave
%dw 2.0
output application/json
// See the documentation for this functions
import decodeURI, decodeURIComponent, encodeURI, encodeURIComponent from dw::core::URL
// Step 1) Enter http://rebrand.ly/dwl-2-playground
// Step 2) Copy and paste this code in the transformation editor
// You now will see the results in the right pane
// Step 3) Implement the parse(str) function
// Once you make a test pass, that test will dissapear
// from the preview pane (cleanPassing function)
/**
* this is the function to implement, the goal is to parse a query strings
* and explore TDD using DataWeave
*/
fun parse(str: String) =
null // You should implement this function until you see no results in the preview ->
// Test library
/**
* Basic assertion
**/
fun mustEqual(a, b) = { given: a, expected: b, pass: a == b }
/**
* Define a test case
**/
fun in(name: String, statements: Array<() -> { pass: Boolean }>) = do {
fun safeExecute(fn) = dw::Runtime::try(() -> fn()) match {
case x if x.success == false -> { pass: false, error: x.error }
case x if true -> x.result
}
var results = log(statements map safeExecute($))
---
{ test: name,
result: results,
pass: results reduce (a, b = true) -> a.pass and b
}
}
/**
* Remove all the passing results
**/
fun cleanPassing(tests) =
tests filter ($.pass == false)
---
cleanPassing([
'should parse query strings starting with a `?`' in [
parse('?foo=bar') mustEqual {
foo: 'bar'
}
],
'should parse query strings starting with a `#`' in [
parse('#foo=bar') mustEqual {
foo: 'bar'
}
],
'should parse query strings starting with a `&`' in [
parse('&foo=bar&foo=baz') mustEqual {
foo: ['bar', 'baz']
}
],
'should parse a query string' in [
parse('foo=bar') mustEqual {
foo: 'bar'
}
],
'should parse multiple query string' in [
parse('foo=bar&key=val') mustEqual {
foo: 'bar',
key: 'val'
}
],
'should parse query string without a value' in [
parse('foo') mustEqual {
foo: null
},
parse('foo&key') mustEqual {
foo: null,
key: null
},
parse('foo=bar&key') mustEqual {
foo: 'bar',
key: null
},
parse('a&a') mustEqual {
a: [null, null]
},
parse('a=&a') mustEqual {
a: ['', null]
}
],
'return empty object if no qss can be found' in [
parse('?') mustEqual {},
parse('&') mustEqual {},
parse('#') mustEqual {},
parse(' ') mustEqual {}
],
'handle `+` correctly' in [
parse('foo+faz=bar+baz++') mustEqual {
'foo faz': 'bar baz '
}
],
'handle multiple of the same key' in [
parse('foo=bar&foo=baz') mustEqual {
foo: ['bar', 'baz']
}
],
'handle multiple values and preserve appearence order' in [
parse('a=value&a=') mustEqual {
a: ['value', '']
},
parse('a=&a=value') mustEqual {
a: ['', 'value']
}
],
'handle multiple values and preserve appearance order with brackets' in [
parse('a[]=value&a[]=') mustEqual {
a: ['value', '']
},
parse('a[]=&a[]=value') mustEqual {
a: ['', 'value']
}
],
'should parse query strings params including embedded `=`' in [
parse('?param=http%3A%2F%2Fsomeurl%3Fid%3D2837') mustEqual {
param: 'http://someurl?id=2837'
}
],
'should parse query strings params including raw `=`' in [
parse('?param=http://someurl?id=2837') mustEqual {
param: 'http://someurl?id=2837'
}
],
'should parse query strings having indexed arrays' in [
parse('foo[0]=bar&foo[1]=baz') mustEqual {
'foo[0]': 'bar',
'foo[1]': 'baz'
}
],
'should parse query strings having brackets arrays' in [
parse('foo[]=bar&foo[]=baz') mustEqual {
'foo[]': ['bar', 'baz']
}
],
'should parse query strings having indexed arrays keeping index order' in [
parse('foo[1]=bar&foo[0]=baz') mustEqual {
'foo[1]': 'bar',
'foo[0]': 'baz'
}
],
'should parse query string having a single bracketed value and format option as `bracket`' in [
parse('foo[]=bar') mustEqual {
foo: ['bar']
}
],
'should parse query string not having a bracketed value and format option as `bracket`' in [
parse('foo=bar') mustEqual {
foo: 'bar'
}
],
'should parse query string having a bracketed value and a single value and format option as `bracket`' in [
parse('foo=bar&baz[]=bar') mustEqual {
foo: 'bar',
baz: ['bar']
}
],
'should parse query strings having brackets arrays and format option as `bracket`' in [
parse('foo[]=bar&foo[]=baz') mustEqual {
foo: ['bar', 'baz']
}
],
'decode keys and values' in [
parse('st%C3%A5le=foo') mustEqual {
"ståle": 'foo'
},
parse('foo=%7B%ab%%7C%de%%7D+%%7Bst%C3%A5le%7D%') mustEqual {
foo: '{%ab%|%de%} %{ståle}%'
}
]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.