/* global atom:true */
const _ = require('underscore-plus');
const clipboard = atom.clipboard;

module.exports =
class KillRing {
  constructor(limit=16) {
    this.buffer = [];
    this.sealed = true;
    this.limit = limit;
  }

  put(texts, forward=true) {
    if(this.sealed)
      this.push(texts);
    else
      this.update(texts, forward);
  }

  seal() {
    this.sealed = true;
  }

  push(texts) {
    this.buffer.push(texts);
    clipboard.write(texts.join('\n'));
    if(this.buffer.length > this.limit) this.buffer.shift();
    this.sealed = false;
  }

  update(texts, forward) {
    const newTexts = _.zip(_.last(this.buffer), texts).map(([l,t])=>
      forward ? (l+(t?t:'')) : ((t?t:'')+l)
    );
    this.buffer.pop();
    this.push(newTexts);
  }

  top() {
    return _.last(this.list());
  }

  list() {
    const lasts = _.last(this.buffer);
    const last = lasts?lasts.join('\n'):'';
    if(clipboard.md5(last) != clipboard.signatureForMetadata)
      this.push(clipboard.read());
    return this.buffer;
  }

};

@@ -602,72 +602,74 @@
 nst 
-newTexts = _.zip(_.last(this.buffer), texts).map((%5Bl,t%5D)=%3E%0A     
+lasts = this.buffer.pop();%0A    const newTexts = texts.map((t,i) =%3E
  for
@@ -675,18 +675,24 @@
 rward ? 
-(
 l
+asts%5Bi%5D
 +(t?t:''
@@ -696,13 +696,11 @@
 :'')
-)
  : 
-(
 (t?t
@@ -709,37 +709,15 @@
 ')+l
-)%0A    );%0A    this.buffer.pop(
+asts%5Bi%5D
 );%0A 

Refactor to simplify