Kijiji Canada Custom Buckets

Code from kijijji.ca to calculate price buckets for header bidding

by Patrick Hund

JavaScript

// Constants for price bucket calculation.
var BRACKET_PENNIES = 3.00,
  BRACKET_NICKELS = 5.00,
  BRACKET_DIMES = 10.00,
  PENNIES_PER_$ = 100,
  NICKELS_PER_$ = 20,
  DIMES_PER_$ = 10,
  QUARTERS_PER_$ = 4;

const Kj = {
  Banners: {
    Bidding: {
      /**
       * @name padPriceBucket
       * Given a particular number, add leading zeroes to make it a 3-char ID string.
       * Combine our number (bucket) with a large number (1e15) and cast to string.
       * Then use slice to cut the leading "1"
       * https://gist.github.com/aemkei/1180489#file-annotated-js
       * @param {Number} Numeric value to be converted.
       * @returns {String}
       */
      padPriceBucket: function(bucket) {
        return (1e15 + bucket + '').slice(-3);
      },

      /**
       * @name getCustomPriceBucket
       * Given a CPM from a bidding partner, compute/identify the DFP price bucket.
       * IDs are sequential values starting at 1¢ (e.g., "001", "002", "003", ...).
       * If all price buckets were penny for penny, the ID is simple to derive.
       * For example, a CPM of 59¢ would go in the price bucket "059" (0.59 * 100).
       * But, at different thresholds, the buckets are by the nickel, dime, or quarter.
       * Therefore, an approach similar to income tax bracketing is used.
       * So, we can count increments across thresholds and keep the IDs sequential.
       * The price bucket IDs and CPM thresholds are devised by Ad Ops.
       * @param {Number} Price/bid/CPM to be bucketed.
       * @returns {String}
       */
      getCustomPriceBucket: function(cpm) {
        var count = 0;

        // Pre-cache some calculations.
        var PENNIES = BRACKET_PENNIES * PENNIES_PER_$,
          NICKELS = (BRACKET_NICKELS - BRACKET_PENNIES) * NICKELS_PER_$,
          DIMES = (BRACKET_DIMES - BRACKET_NICKELS) * DIMES_PER_$;

        if (cpm < BRACKET_PENNIES) {
          count = cpm * PENNIES_PER_$;
        } else if (cpm < BRACKET_NICKELS) {
          count =...