Provides simple and the most useful methods to string operations in JavaScript / Node.js

Overview

🔪 Strops (String Operations)

npm version Known Vulnerabilities FOSSA Status

Provides simple methods for the most useful operations with substrings:
- remove, replace, get from A to B, get from A to B exclusive and other.
For an example, you can use it for an HTML page repairing or restyling old websites:
- replacing CSS styles, appending blocks into html, deleting specific tags with inners, and etc.

Of course, these methods cannot give you total control as RegExp. But in many cases it can be more easy and efficient.
This package has zero dependencies.

😉 Hope you enjoy it!

📌 Installation

$ npm i strops

Node.js

const str = require('strops')
// or you can import just the methods you need:
const { replace, remove } = require('strops')

Methods

Method Description Params
remove Removes all substrings and returns <string> <string, ...substrings>
replace Replaces all substrings with new substring and returns <string> <string, newSubstring, ...substrings>
getAtoB Returns all substrings from A to B as an <array> of <string> <string, substring_A, substring_B>
getAtoBInner Returns all substrings from A to B as an <array> of <string> exclusive <string, substring_A, substring_B>
removeAtoB Removes all substrings from A to B as a <string> <string, substring_A, substring_B>
removeAtoBInner Removes all substrings from A to B as a <string> exclusive <string, substring_A, substring_B>
getIndexes Returns index pairs for all substrings as a <key: startIndex, value: endIndex> <string, ...substrings>
getIndexesAtoB Returns index pairs for all substrings from A to B as a <key: startIndex, value: endIndex> <string, substring_A, substring_B>
getIndexesAtoBInner Returns index pairs for all substrings from A to B as a <key: startIndex, value: endIndex> exclusive <string, substring_A, substring_B>

📘 Usage

Common snippet for examples below:

const str = require('strops')

const text = `<table>
<tr><td class="class-1"><b>JavaScript</b></td></tr>
<tr><td class="class-2">C++</td></tr>
<tr><td class="class-1">Python</td></tr>
</table>`

remove:

const newText = str.remove(text, '<b>', '</b>', 'class-1')

// returns:
// <table>
// <tr><td class="">JavaScript</td></tr>
// <tr><td class="class-2">C++</td></tr>
// <tr><td class="">Python</td></tr>
// </table>

// Also you can pass an array of substrings to get the same result
// Just use the spread operator to expand the array out to the parameters
const newText = str.remove(text, ...['<b>', '</b>', 'class-1'])

removeAtoB:

const newText = str.removeAtoB(text, '<tr>', '</tr>')

// returns:
// <table>
//
//
//
// </table>

removeAtoBInner:

const newText = str.removeAtoBInner(text, '<tr>', '</tr>')

// returns:
// <table>
// <tr></tr>
// <tr></tr>
// <tr></tr>
// </table>

replace:

const newText = str.replace(text, 'Rust', 'Python', 'C++', 'JavaScript')

// returns:
// <table>
// <tr><td class="class-1"><b>Rust</b></td></tr>
// <tr><td class="class-2">Rust</td></tr>
// <tr><td class="class-1">Rust</td></tr>
// </table>

getAtoB:

const newText = str.getAtoB(text, '<tr>', '</tr>')

// returns:
// [
//  '<tr><td class="class-1"><b>JavaScript</b></td></tr>',
//  '<tr><td class="class-2">C++</td></tr>',
//  '<tr><td class="class-1">Python</td></tr>'
// ]

// You can easily get first entry by:

const newText = str.getAtoB(text, '<tr>', '</tr>')[0]

// returns:
// <tr><td class="class-1"><b>JavaScript</b></td></tr>

getAtoBInner:

const newText = str.getAtoBInner(text, '<tr>', '</tr>')

// returns:
// [
//  '<td class="class-1"><b>JavaScript</b></td>',
//  '<td class="class-2">C++</td>',
//  '<td class="class-1">Python</td>'
// ]

getIndexes:

const newText = str.getIndexes(text, '<tr>')

// returns:
// { '8': 12, '60': 64, '99': 103 }
// * where <key> is start index, and <value> is an end index

// And yes. I know that we have <key> as a type of <string> here.
// But this approach simple to use when you get slices by iterating this:
const word = 'Hey bro!'
const indexes = { 0: 3, 4: 7 }

for (let key in indexes) {
	console.log(word.substring(key, indexes[key]))
}
// Hey
// bro

getIndexesAtoBInner:

const newText = str.getIndexesAtoBInner(text, '<tr>', '</tr>')

// returns:
// { '12': 54, '64': 93, '103': 134 }
// * where <key> is start index, and <value> is an end index

getIndexesAtoB:

const newText = str.getIndexesAtoB(text, '<tr>', '</tr>')

// returns:
// { '8': 59, '60': 98, '99': 139 }
// * where <key> is start index, and <value> is an end index

🎯 Coming soon:

  • Tests
  • Methods with a simple conditions/RegExp integration
  • Specific methods for an HTML tags

License

Copyright (c) 2022 Max Shane. Strops is MIT licensed.

You might also like...

Solid Forms provides several form control objects useful for making working with forms easier.

Solid Forms Solid Forms provides several form control objects useful for making working with forms easier. Demos and examples below. # solidjs yarn ad

Jan 2, 2023

In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. All the elements of the user interface are fully functional.

In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. All the elements of the user interface are fully functional.

To Do list: add & remove In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. All the elements of th

Jan 3, 2023

A simple project to keep track of your activities. It allows basic CRUD operations to organize your to do list.

todo-list A simple project to keep track of todo list. Built With HTML CSS Javascript Webpack Live Demo You can checkout the live version here Getting

Jul 20, 2022

It's a javascript Class which contains utility methods that simplify working with google maps web SDK

About GoogleMapsJSHelper It's a javascript Class which contains utility methods that simplify working with google maps web SDK Note: i used ES7 Class

Jul 23, 2022

Boiler is a utility library that makes every day tasks in JavaScript easier by providing over 115 methods

Boiler is a utility library that makes every day tasks in JavaScript easier by providing over 115 methods that work on arrays, collections, functions, numbers, objects, and strings. It's the JavaScript equivalent of a Swiss Army Knife.

Nov 1, 2022

Strcmp-node - A cli string comparison tool, because apparently one doesn't exist.

Strcmp-node - A cli string comparison tool, because apparently one doesn't exist.

strcmp-node I couldn't find a string comparison command, so i made my own. its probably the ugliest thing since godzilla with makeup on, but it works.

Jan 1, 2022

Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Jan 6, 2023

A simple code that creates a string of random characters displayed in an html object, all saving in a json file.

A simple code that creates a string of random characters displayed in an html object, all saving in a json file.

I'm 17 Years Old Developer / Lead Developer. 💻 I'm wroking on AdrenalinaRP, GrandRDM. 🪙 I’m currently learning JavaScript. 🫂 I’m looking to collabo

Nov 17, 2022

This branch is created to make receive and send data to api using async and await methods

This branch is created to make receive and send data to api using async and await methods

Microverse-Leader-Board Project from module 2 week 4 This branch is created to make receive and send data to api using async and await methods Screens

Apr 22, 2022
Comments
Owner
Max Shane
Node.js / Javascript developer
Max Shane
A simple in-memory key-value cache for function execution, allowing both sync and async operations using the same methods

A simple in-memory key-value cache for function execution, allowing both sync and async operations using the same methods. It provides an invalidation mechanism based both on exact string and regex.

cadienvan 10 Dec 15, 2022
Convert some JavaScript/TypeScript code string into a .d.ts TypeScript Declaration code string

convert-to-dts Converts the source code for any .js or .ts file into the equivalent .d.ts code TypeScript would generate. Usage import { convertToDecl

Lily Scott 11 Mar 3, 2022
A small (~600B gzip), useful set of methods for lazy iteration of iterables.

@ricokahler/lazy · A small (~600B gzip*), useful set of methods for lazy iteration of iterables. Why this lazy lib? Do I even need a lazy lib? Install

Rico Kahler 11 Sep 10, 2022
A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Cloudflare 18 Dec 4, 2022
The invoker based on event model provides an elegant way to call your methods in another container via promisify functions

The invoker based on event model provides an elegant way to call your methods in another container via promisify functions. (like child-processes, iframe, web worker etc).

尹挚 7 Dec 29, 2022
Leader Board is a simple project based on JavaScript programing language. The purpose of this project is to work with APIs and ASYNC & AWAIT methods. I have used vanilla JavaScript with web pack to implement this project

Leader Board - JavaScript Project Table of contents Overview The challenge Screenshot Links Project Setup commands My process Built with What I learne

Mahdi Rezaei 7 Oct 21, 2022
A UI library by WeChat official design team, includes the most useful widgets/modules in mobile web applications.

WeUI - tailor-made for WeChat web service 中文版本 Introduction WeUI is an WeChat-like UI framework officially designed by the WeChat Design Team, tailor-

Tencent 26.6k Jan 2, 2023
A UI library by WeChat official design team, includes the most useful widgets/modules.

WeUI for 小程序 为微信小程序量身设计 概述 WeUI 是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信内网页和微信小程序量身设计,令用户的使用感知更加统一。包含button、cell、dialog、 progress、 toast、article、actionsheet、

Tencent 14.5k Jan 4, 2023
Repository trying to recreate Javascript (node) methods.

Javascript Polyfills This is an open-source project, where we try to do a polyfill for every (or almost all of them) Javascript Methods that currently

Nícolas Gabriel 9 Oct 8, 2022
In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. I followed the JavaScript, CSS, HTML, DRY, KISS and YAGNI Best practices.

To Do list: add & remove In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. All the elements of th

Olivier 6 Nov 20, 2022