asData improvement
asBuffer to transform UintArray to node Buffer Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
8b39902910
commit
16d9bfb7e3
|
@ -33,18 +33,39 @@ const equalsOrThrow = (v1, v2, t) =>
|
||||||
const isBase64 = (value) =>
|
const isBase64 = (value) =>
|
||||||
Buffer.from(value, "base64").toString("base64") === value;
|
Buffer.from(value, "base64").toString("base64") === value;
|
||||||
|
|
||||||
|
const isBuffer = (value) =>
|
||||||
|
value instanceof Buffer;
|
||||||
|
|
||||||
|
const asBuffer = (value) =>
|
||||||
|
isBinary(value)
|
||||||
|
? Buffer.from(value)
|
||||||
|
: isBuffer(value)
|
||||||
|
? value
|
||||||
|
: (() => {throw {message: "is not buffer or a valid binary"}})();
|
||||||
|
|
||||||
|
const asBase64 = (value) =>
|
||||||
|
asBuffer(value).toString("base64");
|
||||||
|
|
||||||
const clone = (o) =>
|
const clone = (o) =>
|
||||||
JSON.parse(JSON.stringify(o));
|
JSON.parse(JSON.stringify(o));
|
||||||
|
|
||||||
const isJsonContentType = (contentType) =>
|
const isJsonContentType = (contentType) =>
|
||||||
contentType && contentType.match(/(json)/i);
|
contentType && contentType.match(/(json)/i);
|
||||||
|
|
||||||
const asData = (data, contentType) =>
|
const asData = (data, contentType) => {
|
||||||
((typeof data) !== "string"
|
let result = data;
|
||||||
? data
|
|
||||||
: isJsonContentType(contentType)
|
// pattern matching alike
|
||||||
? JSON.parse(data)
|
result = isString(result) && isJsonContentType(contentType)
|
||||||
: data);
|
? JSON.parse(result)
|
||||||
|
: result;
|
||||||
|
|
||||||
|
result = isBinary(result)
|
||||||
|
? asBase64(result)
|
||||||
|
: result;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isString,
|
isString,
|
||||||
|
@ -64,5 +85,6 @@ module.exports = {
|
||||||
isBase64,
|
isBase64,
|
||||||
clone,
|
clone,
|
||||||
|
|
||||||
asData
|
asData,
|
||||||
|
asBase64
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue