function padSequences(
    sequences, maxLen, padding = 'pre', truncating = 'pre', value = PAD_INDEX) {
  // TODO(cais): This perhaps should be refined and moved into tfjs-preproc.
  return sequences.map(seq => {
    // Perform truncation.
    if (seq.length > maxLen) {
      if (truncating === 'pre') {
        seq.splice(0, seq.length - maxLen);
      } else {
        seq.splice(maxLen, seq.length - maxLen);
      }
    }

    // Perform padding.
    if (seq.length < maxLen) {
      const pad = [];
      for (let i = 0; i < maxLen - seq.length; ++i) {
        pad.push(value);
      }
      if (padding === 'pre') {
        seq = pad.concat(seq);
      } else {
        seq = seq.concat(pad);
      }
    }

    return seq;
  });
}
Pad and truncate all sequences to the same length

@param {number[][]} sequences The sequences represented as an array of array
  of numbers.
@param {number} maxLen Maximum length. Sequences longer than `maxLen` will be
  truncated. Sequences shorter than `maxLen` will be padded.
@param {'pre'|'post'} padding Padding type.
@param {'pre'|'post'} truncating Truncation type.
@param {number} value Padding value.