text: 'use strict';
  
  var DateFormatter = function (datePattern) {
      var owner = this;
  
      owner.blocks = [];
      owner.datePattern = datePattern;
      owner.initBlocks();
  };
  
  DateFormatter.prototype = {
      initBlocks: function () {
          var owner = this;
          owner.datePattern.forEach(function (value) {
              if (value === 'Y') {
                  owner.blocks.push(4);
              } else {
                  owner.blocks.push(2);
              }
          });
      },
  
      getBlocks: function () {
          return this.blocks;
      },
  
      getValidatedDate: function (value) {
          var owner = this, result = '';
  
          value = value.replace(/[^\d]/g, '');
  
          owner.blocks.forEach(function (length, index) {
              if (value.length > 0) {
                  var sub = value.slice(0, length),
                      sub0 = sub.slice(0, 1),
                      rest = value.slice(length);
  
                  switch (owner.datePattern[index]) {
                  case 'd':
                      if (sub === '00') {
                          sub = '01';
                      } else if (parseInt(sub0, 10) > 3) {
                          sub = '0' + sub0;
                      } else if (parseInt(sub, 10) > 31) {
                          sub = '31';
                      }
  
                      break;
  
                  case 'm':
                      if (sub === '00') {
                          sub = '01';
                      } else if (parseInt(sub0, 10) > 1) {
                          sub = '0' + sub0;
                      } else if (parseInt(sub, 10) > 12) {
                          sub = '12';
                      }
  
                      break;
                  }
  
                  result += sub;
  
                  // update remaining string
                  value = rest;
              }
          });
  
          return result;
      }
  };
  
  module.exports = DateFormatter;
  
  