@@ -1941,97 +1941,98 @@ exports.attribsAttributeValue = (attribs, key, pool) => {
19411941
19421942/**
19431943 * Incrementally builds a Changeset.
1944- *
1945- * @typedef {object } Builder
1946- * @property {Function } insert -
1947- * @property {Function } keep -
1948- * @property {Function } keepText -
1949- * @property {Function } remove -
1950- * @property {Function } toString -
19511944 */
1945+ class Builder {
1946+ /**
1947+ * @param {number } oldLen - Old length
1948+ */
1949+ constructor ( oldLen ) {
1950+ this . _oldLen = oldLen ;
1951+ this . _ops = [ ] ;
1952+ this . _charBank = exports . stringAssembler ( ) ;
1953+ }
19521954
1953- /**
1954- * @param {number } oldLen - Old length
1955- * @returns {Builder }
1956- */
1957- exports . builder = ( oldLen ) => {
1958- const ops = [ ] ;
1959- const charBank = exports . stringAssembler ( ) ;
1955+ /**
1956+ * @param {number } N - Number of characters to keep.
1957+ * @param {number } L - Number of newlines among the `N` characters. If positive, the last
1958+ * character must be a newline.
1959+ * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1960+ * (no pool needed in latter case).
1961+ * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1962+ * attribute key, value pairs.
1963+ * @returns {Builder } this
1964+ */
1965+ keep ( N , L , attribs , pool ) {
1966+ const o = new Op ( '=' ) ;
1967+ o . attribs = ( attribs && exports . makeAttribsString ( '=' , attribs , pool ) ) || '' ;
1968+ o . chars = N ;
1969+ o . lines = ( L || 0 ) ;
1970+ this . _ops . push ( o ) ;
1971+ return this ;
1972+ }
19601973
1961- const self = {
1962- /**
1963- * @param {number } N - Number of characters to keep.
1964- * @param {number } L - Number of newlines among the `N` characters. If positive, the last
1965- * character must be a newline.
1966- * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1967- * (no pool needed in latter case).
1968- * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1969- * attribute key, value pairs.
1970- * @returns {Builder } this
1971- */
1972- keep : ( N , L , attribs , pool ) => {
1973- const o = new Op ( '=' ) ;
1974- o . attribs = ( attribs && exports . makeAttribsString ( '=' , attribs , pool ) ) || '' ;
1975- o . chars = N ;
1976- o . lines = ( L || 0 ) ;
1977- ops . push ( o ) ;
1978- return self ;
1979- } ,
1974+ /**
1975+ * @param {string } text - Text to keep.
1976+ * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1977+ * (no pool needed in latter case).
1978+ * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1979+ * attribute key, value pairs.
1980+ * @returns {Builder } this
1981+ */
1982+ keepText ( text , attribs , pool ) {
1983+ this . _ops . push ( ...opsFromText ( '=' , text , attribs , pool ) ) ;
1984+ return this ;
1985+ }
19801986
1981- /**
1982- * @param {string } text - Text to keep.
1983- * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1984- * (no pool needed in latter case).
1985- * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1986- * attribute key, value pairs.
1987- * @returns {Builder } this
1988- */
1989- keepText : ( text , attribs , pool ) => {
1990- ops . push ( ...opsFromText ( '=' , text , attribs , pool ) ) ;
1991- return self ;
1992- } ,
1987+ /**
1988+ * @param {string } text - Text to insert.
1989+ * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1990+ * (no pool needed in latter case).
1991+ * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1992+ * attribute key, value pairs.
1993+ * @returns {Builder } this
1994+ */
1995+ insert ( text , attribs , pool ) {
1996+ this . _ops . push ( ...opsFromText ( '+' , text , attribs , pool ) ) ;
1997+ this . _charBank . append ( text ) ;
1998+ return this ;
1999+ }
19932000
1994- /**
1995- * @param {string } text - Text to insert.
1996- * @param {(string|Attribute[]) } attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...'
1997- * (no pool needed in latter case).
1998- * @param {?AttributePool } pool - Attribute pool, only required if `attribs` is a list of
1999- * attribute key, value pairs.
2000- * @returns {Builder } this
2001- */
2002- insert : ( text , attribs , pool ) => {
2003- ops . push ( ...opsFromText ( '+' , text , attribs , pool ) ) ;
2004- charBank . append ( text ) ;
2005- return self ;
2006- } ,
2001+ /**
2002+ * @param {number } N - Number of characters to remove.
2003+ * @param {number } L - Number of newlines among the `N` characters. If positive, the last
2004+ * character must be a newline.
2005+ * @returns {Builder } this
2006+ */
2007+ remove ( N , L ) {
2008+ const o = new Op ( '-' ) ;
2009+ o . attribs = '' ;
2010+ o . chars = N ;
2011+ o . lines = ( L || 0 ) ;
2012+ this . _ops . push ( o ) ;
2013+ return this ;
2014+ }
20072015
2008- /**
2009- * @param {number } N - Number of characters to remove.
2010- * @param {number } L - Number of newlines among the `N` characters. If positive, the last
2011- * character must be a newline.
2012- * @returns {Builder } this
2013- */
2014- remove : ( N , L ) => {
2015- const o = new Op ( '-' ) ;
2016- o . attribs = '' ;
2017- o . chars = N ;
2018- o . lines = ( L || 0 ) ;
2019- ops . push ( o ) ;
2020- return self ;
2021- } ,
2022-
2023- toString : ( ) => {
2024- /** @type {number } */
2025- let lengthChange ;
2026- const serializedOps = exports . serializeOps ( ( function * ( ) {
2027- lengthChange = yield * exports . canonicalizeOps ( ops , true ) ;
2028- } ) ( ) ) ;
2029- const newLen = oldLen + lengthChange ;
2030- return exports . pack ( oldLen , newLen , serializedOps , charBank . toString ( ) ) ;
2031- } ,
2032- } ;
2016+ toString ( ) {
2017+ /** @type {number } */
2018+ let lengthChange ;
2019+ const serializedOps = exports . serializeOps ( ( function * ( ) {
2020+ lengthChange = yield * exports . canonicalizeOps ( this . _ops , true ) ;
2021+ } ) . call ( this ) ) ;
2022+ const newLen = this . _oldLen + lengthChange ;
2023+ return exports . pack ( this . _oldLen , newLen , serializedOps , this . _charBank . toString ( ) ) ;
2024+ }
2025+ }
2026+ exports . Builder = Builder ;
20332027
2034- return self ;
2028+ /**
2029+ * @deprecated Use the `Builder` class instead.
2030+ * @param {number } oldLen - Old length
2031+ * @returns {Builder }
2032+ */
2033+ exports . builder = ( oldLen ) => {
2034+ warnDeprecated ( 'Changeset.builder() is deprecated; use the Changeset.Builder class instead' ) ;
2035+ return new Builder ( oldLen ) ;
20352036} ;
20362037
20372038exports . makeAttribsString = ( opcode , attribs , pool ) => {
@@ -2128,7 +2129,7 @@ exports.inverse = (cs, lines, alines, pool) => {
21282129 let curLineNextOp = new Op ( '+' ) ;
21292130
21302131 const unpacked = exports . unpack ( cs ) ;
2131- const builder = exports . builder ( unpacked . newLen ) ;
2132+ const builder = new Builder ( unpacked . newLen ) ;
21322133
21332134 const consumeAttribRuns = ( numChars , func /* (len, attribs, endsLine)*/ ) => {
21342135 if ( ! curLineOps || curLineOpsLine !== curLine ) {
0 commit comments