Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Created May 22, 2019 15:46
Show Gist options
  • Save dinocarl/56005623642b138172608c71a0b2409f to your computer and use it in GitHub Desktop.
Save dinocarl/56005623642b138172608c71a0b2409f to your computer and use it in GitHub Desktop.
const basicSurround = curry((char, text) => `${char}${text}${char}`);
const snglQt = basicSurround(`'`);
const dblQt = basicSurround(`"`);
const OrderPrefixTmplt = jsonKey => `${dblQt(jsonKey)}->>`;
const OrderBaseTmplt = (field, dir) => `${field} ${dir}`;
const OrderTmplt = ( [field, dir, jsonKey] ) => isNil(jsonKey)
? OrderBaseTmplt(dblQt(field), dir)
: `${OrderPrefixTmplt(jsonKey)}${OrderBaseTmplt(snglQt(field), dir)}`
;
// Returns 3 item array that can be applied to OrderTmplt
const makeOrderTmpltTuple = ( [fieldName, dir] ) => {
// Because Array destructuring returns undefined for unreturnable
// positions, placing the jsonKey assignment at the end ensures
// that it either returns undefined or the actual name of the
// desired json keyname in the last position of the array
let [field, jsonKey] = reverse(
split('.', fieldName)
);
return [
field,
dir,
jsonKey,
];
};
const createOrderStr = compose(
OrderTmplt,
makeOrderTmpltTuple
);
const createOrder = compose(
join(', '),
map(createOrderStr),
propOr([['createdAt', 'DESC']], 'order')
);
// -- process
const order = [
['data.date', 'DESC'],
['data.title', 'ASC'],
['updatedAt', 'DESC']
];
const data = {
order
};
createOrder(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment