UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

77 lines 2.6 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); exports.Default = "default"; function isExactly(b) { const maybe = b; return !!maybe && maybe.exactly !== undefined; } function isUpTo(b) { const maybe = b; return !!maybe && maybe.upTo !== undefined; } var BandCasing; (function (BandCasing) { BandCasing[BandCasing["NoChange"] = 0] = "NoChange"; BandCasing[BandCasing["Sentence"] = 1] = "Sentence"; })(BandCasing = exports.BandCasing || (exports.BandCasing = {})); /** * Return the band for the given value * @param {Bands} bands * @param {number} value * @return {string} */ function bandFor(bands, value, options = { includeNumber: false, casing: BandCasing.NoChange }) { const bandNames = Object.getOwnPropertyNames(bands); for (const bandName of bandNames) { const band = bands[bandName]; if (isExactly(band) && band.exactly === value) { return format(value, bandName, band, options); } } const upToBands = _.sortBy(bandNames .map(name => ({ name, band: bands[name] })) .filter(nb => isUpTo(nb.band)), b => b.band.upTo); for (const upTo of upToBands) { if (upTo.band.upTo > value) { return format(value, upTo.name, upTo.band, options); } } return formatName(bandNames.find(n => bands[n] === exports.Default), options); } exports.bandFor = bandFor; function format(value, name, band, options) { const includeNumber = options.includeNumber; const fName = formatName(name, options); if (includeNumber && isExactly(band)) { return `${fName} (=${band.exactly})`; } if (includeNumber && isUpTo(band)) { return `${fName} (<${band.upTo})`; } return fName; } function formatName(name, options = {}) { if (options.casing === BandCasing.Sentence) { return _.upperFirst(name); } else { return name; } } //# sourceMappingURL=bands.js.map