mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-03-10 22:44:29 +00:00
Convert conversions module to typescript
Change-type: patch Signed-off-by: Cameron Diver <cameron@resin.io>
This commit is contained in:
parent
367e90d6e4
commit
9150c3fdbc
@ -1,12 +0,0 @@
|
||||
_ = require 'lodash'
|
||||
|
||||
exports.envArrayToObject = (env) ->
|
||||
# env is an array of strings that say 'key=value'
|
||||
toPair = (keyVal) ->
|
||||
m = keyVal.match(/^([^=]+)=(.*)$/)
|
||||
if !m?
|
||||
console.log("WARNING: Could not correctly parse env var #{keyVal}. " +
|
||||
'Please fix this var and recreate the container.')
|
||||
return null
|
||||
return m[1..]
|
||||
_(env).map(toPair).filter(([_, v]) -> v?).fromPairs().value()
|
21
src/lib/conversions.ts
Normal file
21
src/lib/conversions.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { EnvVarObject } from './types';
|
||||
|
||||
export function envArrayToObject(env: string[]): EnvVarObject {
|
||||
const toPair = (keyVal: string) => {
|
||||
const m = keyVal.match(/^([^=]+)=(.*)$/);
|
||||
if (m == null) {
|
||||
console.log(`WARNING: Could not correctly parse env var ${keyVal}. ` +
|
||||
'Please fix this var and recreate the container.');
|
||||
return [null, null];
|
||||
}
|
||||
return m.slice(1);
|
||||
};
|
||||
|
||||
return _(env)
|
||||
.map(toPair)
|
||||
.filter(([_, v]) => v != null)
|
||||
.fromPairs()
|
||||
.value();
|
||||
}
|
38
test/15-conversions.spec.coffee
Normal file
38
test/15-conversions.spec.coffee
Normal file
@ -0,0 +1,38 @@
|
||||
m = require 'mochainon'
|
||||
{ expect } = m.chai
|
||||
|
||||
conversion = require '../src/lib/conversions'
|
||||
|
||||
describe 'conversions', ->
|
||||
|
||||
describe 'envArrayToObject', ->
|
||||
it 'should convert an env array to an object', ->
|
||||
expect(conversion.envArrayToObject([
|
||||
'key=value'
|
||||
'test1=test2'
|
||||
'k=v'
|
||||
'equalsvalue=thisvaluehasan=char'
|
||||
'asd='
|
||||
'number=123'
|
||||
])).to.deep.equal({
|
||||
key: 'value'
|
||||
test1: 'test2'
|
||||
k: 'v'
|
||||
equalsvalue: 'thisvaluehasan=char'
|
||||
asd: ''
|
||||
number: '123'
|
||||
})
|
||||
|
||||
it 'should ignore invalid env array entries', ->
|
||||
expect(conversion.envArrayToObject([
|
||||
'key1',
|
||||
'key1=value1'
|
||||
])).to.deep.equal({
|
||||
key1: 'value1'
|
||||
})
|
||||
|
||||
it 'should return an empty object with an empty input', ->
|
||||
expect(conversion.envArrayToObject(null)).to.deep.equal({})
|
||||
expect(conversion.envArrayToObject('')).to.deep.equal({})
|
||||
expect(conversion.envArrayToObject([])).to.deep.equal({})
|
||||
expect(conversion.envArrayToObject(1)).to.deep.equal({})
|
Loading…
x
Reference in New Issue
Block a user