Giter VIP home page Giter VIP logo

stack-based-text-editor's Introduction

TEXT-EDITOR

Introduction

I have made this project to apply the concept of stack datastructure to create a basic text editor.

Basic Idea

The two main characteristics of a stack datastructure are it's push and pull operations.

If you notice carefully,in a text editor,the most basic operations include inserting element in a sequential order(i.e one after an another) and deleting them one by one. Also, in a text editor, you are able to perform the undo operation to retrieve the elements you deleted by mistake.

So, my idea was to use the push, pull operations to achieve these 3 basic tasks.

The Logic

Inside stack.js

Using Buffer

In the stack constructor, I have included a buffer.

this.buffer = 4;
  • Buffer objects are used to represent a fixed length sequence of bytes.
  • It restricts the the number of undo operations done by the stack.

So basically, it makes sure that the undo operations are performed sequentially.

The Push() operation

The push operation used in this project is similar to the basic push() operation of a stack with some slight changes.

    push(type, char){
        if(this.isEmpty()){
            if(type===0)
                this.stack.push([type, char]);
        } else{
            let
            tmp = this.top();
            if(tmp[0]===type && tmp[1].length < this.buffer){
                let top = this.pop();
                top[1] = char + top[1];
                this.stack.push(top);
            } else{
                this.stack.push([type, char]);
            }
        }
        this.size++;
    }
  • The type variable is used to store two kinds of operations
    • push - 0
    • pop - 1
  • It was important to differentiate the two operations in this function like this as the undo operation messes up the order in which the elements were inserted in the stack, once they are popped.
  • Therefore, if we want to push back the popped elements using the undo operation, we need to reverse the order
    if(tmp[0]===type && tmp[1].length < this.buffer){
               let top = this.pop();
               top[1] = char + top[1];
               this.stack.push(top);
           }

So, the type value decides whether we need to reverse the order of the elements pushed in the stack after we use the undo operation.

stack-based-text-editor's People

Contributors

piyushkhandelwal993 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.