Skip to content
Commits on Source (2)
const expose = require('./expose');
const DISPLAYSHORT = 2;
const MONTHCORRECTION = 1;
/**
* @param value
* @return {string}
*/
const padDate = value =>
value
.toString()
.padStart(DISPLAYSHORT, '0');
/**
* @param date
* @return {Date}
*/
const getDate = date =>
new Date(date);
/**
* Return the date of an ISO formatted dateTime in human readable (dutch) DD:MM:YYYY
*
* @param iso
* @return {string}
*/
function createDate(iso) {
const date = getDate(iso);
const year = date.getFullYear();
const month = padDate(date.getMonth() + MONTHCORRECTION);
const day = padDate(date.getDate());
return `${day}-${month}-${year}`;
}
/**
* Return the time of an ISO formatted dateTime in human readable HH:MM:SS
*
* @param iso
* @return {string}
*/
function createTime (iso) {
const date = getDate(iso);
const hours = padDate(date.getHours());
const minutes = padDate(date.getMinutes());
const seconds = padDate(date.getSeconds());
return `${hours}:${minutes}:${seconds}`;
}
module.exports = expose({
createDate,
createTime,
});
const test = require('tape');
const {
createDate,
createTime,
} = require('./formatIso');
test('createDate()', assert => {
const iso = '2018-01-13T10:58:59Z';
const actual = createDate(iso);
const expected = '13-01-2018';
const message = 'returns only the dutch formatted date from an dateTimeIso string';
assert.equal(actual, expected, message);
assert.end();
});
test('createTime()', assert => {
const iso = '2018-01-13T10:58:59Z';
const actual = createTime(iso);
const expected = '10:58:59';
const message = 'returns only the dutch formatted time from an dateTimeIso string';
assert.equal(actual, expected, message);
assert.end();
});
......@@ -6,6 +6,16 @@ const unique = require('./unique');
const { callOrNothingAtAll, passOrGet } = require('./function');
const { bind } = require('./instance');
const { reduceMap } = require('./iterable');
const {
createDate,
createTime,
} = require('./formatIso');
const {
buildParamString,
buildUrl,
getSegment,
objectifyParams,
} = require('./url');
const {
cloneWithout,
extract,
......@@ -19,15 +29,21 @@ const {
module.exports = expose({
bind,
buildParamString,
buildUrl,
callOrNothingAtAll,
cloneWithout,
createDate,
createTime,
dictionary,
expose,
extract,
filterProperties,
get,
getObject,
getSegment,
isObject,
objectifyParams,
passOrGet,
performGetOnProperties,
purge,
......
......@@ -8,6 +8,10 @@ test('📦 @mintlab/kitchen-sink', assert => {
'function',
'has a `callOrNothingAtAll` method'
);
assert.equal(typeof publicApi.buildParamString, 'function', 'has an `buildParamString` method');
assert.equal(typeof publicApi.buildUrl, 'function', 'has an `buildUrl` method');
assert.equal(typeof publicApi.createDate, 'function', 'has an `createDate` method');
assert.equal(typeof publicApi.createTime, 'function', 'has an `createTime` method');
assert.equal(
typeof publicApi.cloneWithout,
'function',
......@@ -24,11 +28,13 @@ test('📦 @mintlab/kitchen-sink', assert => {
assert.equal(typeof publicApi.filterProperties, 'function', 'has an `filterProperties` method');
assert.equal(typeof publicApi.get, 'function', 'has a `get` method');
assert.equal(typeof publicApi.getObject, 'function', 'has a `getObject` method');
assert.equal(typeof publicApi.getSegment, 'function', 'has an `getSegment` method');
assert.equal(
typeof publicApi.passOrGet,
'function',
'has a `passOrGet` method'
);
assert.equal(typeof publicApi.objectifyParams, 'function', 'has an `objectifyParams` method');
assert.equal(typeof publicApi.performGetOnProperties, 'function', 'has a `performGetOnProperties` method');
assert.equal(typeof publicApi.purge, 'function', 'has a `purge` method');
assert.equal(
......
const { get } = require('./object');
const expose = require('./expose');
const { keys} = Object;
/**
* Get the second path segment from an absolute path with an optional query.
*
* @param {string} path
* @return {string}
*/
function getSegment(path) {
const [pathComponent] = path.split('?');
const [, , segment] = pathComponent.split('/');
return segment;
}
/**
* Build a paramString from an the keyValues of an object
*
* @param {Object} params
* @return {string}
*/
function buildParamString(params) {
const paramsAsString =
keys(params)
.filter(param => params[param])
.map(param => `${param}=${encodeURIComponent(get(params, param))}`)
.join('&');
const preParam = paramsAsString.length ? '?' : '';
return `${preParam}${paramsAsString}`;
}
/**
* Build a URL from the base url and given params
*
* @param {string} url
* @param {Object} params
* @return {string}
*/
function buildUrl(url, params) {
return `${url}${buildParamString(params)}`;
}
/**
* @param {string} params
* @return {Object}
*/
const objectifyParams = params => {
if(!params) {
return {};
}
return params
.split('&')
.reduce((acc, param) => {
const [name, value] = param.split('=');
acc[name] = value;
return acc;
}, {});
};
module.exports = expose({
getSegment,
buildParamString,
buildUrl,
objectifyParams,
});
const test = require('tape');
const {
buildParamString,
buildUrl,
getSegment,
objectifyParams,
} = require('./url');
test('getSegment()', assert => {
{
const actual = getSegment('/foo/bar/baz');
const expected = 'bar';
const message = 'returns the second segment of a given path component';
assert.equal(actual, expected, message);
}
{
const actual = getSegment('/foo/bar?baz');
const expected = 'bar';
const message = 'returns the second segment of a given path and query component';
assert.equal(actual, expected, message);
}
assert.end();
});
test('buildParamString()', assert => {
{
const params = {
one: 'first',
two: 'second',
three: 'third',
};
const actual = buildParamString(params);
const expected = '?one=first&two=second&three=third';
const message = 'returns the params of an object as a string with ?-prefix';
assert.equal(actual, expected, message);
}
{
const params = {};
const actual = buildParamString(params);
const expected = '';
const message = 'returns an empty string for an empty object';
assert.equal(actual, expected, message);
}
assert.end();
});
const url = '/api/v1/test';
test('buildUrl()', assert => {
{
const params = {
one: 'first',
two: 'second',
three: 'third',
};
const actual = buildUrl(url, params);
const expected = `${url}?one=first&two=second&three=third`;
const message = 'returns the given path with params';
assert.equal(actual, expected, message);
}
{
const params = {
one: 'f&irst',
two: 's:econd',
three: 't?hird',
};
const actual = buildUrl(url, params);
const expected = `${url}?one=f%26irst&two=s%3Aecond&three=t%3Fhird`;
const message = 'returns the given path with encoded params';
assert.equal(actual, expected, message);
}
{
const params = {
one: 'first',
two: '',
three: null,
four: 'fourth',
};
const actual = buildUrl(url, params);
const expected = `${url}?one=first&four=fourth`;
const message = 'returns the given path with params that have a value';
assert.equal(actual, expected, message);
}
assert.end();
});
test('objectifyParams()', assert => {
const paramString = 'foo=FOO&bar=BAR&quux=QUUX';
const actual = objectifyParams(paramString);
const expected = {
foo: 'FOO',
bar: 'BAR',
quux: 'QUUX',
};
const message = 'transforms a URL string of params to an object with key value pairs';
assert.deepEqual(actual, expected, message);
assert.end();
});