Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/somnathdevpro/swoop.js

A lightweight,versatile javascript utility library for modern web development
https://github.com/somnathdevpro/swoop.js

dom-library frontend javascript javascript-library js library lodash math-functions testing utility-library

Last synced: 7 days ago
JSON representation

A lightweight,versatile javascript utility library for modern web development

Awesome Lists containing this project

README

        

SWOOP.JS : simplifying web development with only one library

Tired of juggling with multiple libraries and utilities for your projects? Don't worry,swoop.js is here to help! A lightweight versatile javascript utility library,containing a ton of useful functions,that simplifies the process of web development,
Swoop js has a comprehensive set of features,from common utilities to to advanced and more specific utility classes,such as:
SWOOPHYSIX for making physics calculations and unit conversions.


SWOOP TEST for unit testing, helping you to write test cases for your code.


SWOOP CACHE to store values for use later,with TTL feature.

INSTALLATION


you can simply include swoop.js using our CDN link: https://cdn.jsdelivr.net/gh/SomnathDevPro/Swoop.js@main/swoop.min.js

USAGE


Let's get started with swoop.js!
include swoop.js to your source code to get started :


The swoop object


The SWOOP object is the main object which you will be using to access the basic utilities!
available utility functions:

ARR (Array Functions)

1. `union(arr1, arr2)`: Returns the union of two arrays.
2. `intersection(arr1, arr2)`: Returns the intersection of two arrays.
3. `difference(arr1, arr2)`: Returns the difference of two arrays.
4. `sum(arr)`: Returns the sum of all elements in an array.
5. `mean(arr)`: Returns the mean of an array.
6. `standard_deviation(arr)`: Returns the standard deviation of an array.
7. `variance(arr)`: Returns the variance of an array.
8. `median(arr)`: Returns the median of an array.
9. `min(arr)`: Returns the minimum value of an array.
10. `max(arr)`: Returns the maximum value of an array.
11. `countUnique(arr)`: Returns the number of unique values in an array.
12. `countDuplicates(arr)`: Returns the number of duplicate values in an array.
13. `merge(arr1, arr2)`: Merges two arrays.
14. `freq(arr)`: Returns the frequency of elements in an array.

DOM (Document Object Model Functions)

1. `select(selector)`: Selects an element by its selector.
2. `selectid(id)`: Selects an element by its ID.
3. `selectall(element)`: Selects all elements with a matching selector.
4. `addListener(element, event, fn)`: Attaches an event listener to an element.
5. `removeListener(element, event, fn)`: Removes an event listener from an element.

FUNC (Functional Programming Utilities)

1. `throttle(func, wait)`: Throttles a function to prevent excessive calls.
2. `debounce(func, wait)`: Debounces a function to prevent excessive calls.
3. `memoize(func)`: Memoizes a function to cache its results.
4. `curry(func)`: Curries a function to enable partial application.
5. `once(func)`: Ensures a function is called only once.

MATHS (Mathematical Functions)

1. `sin(angle)`: Returns the sine of an angle.
2. `cos(angle)`: Returns the cosine of an angle.
3. `tan(angle)`: Returns the tangent of an angle.
4. `distance(x1, x2, y1, y2)`: Returns the distance between two points.
5. `midpoint(x1, x2, y1, y2)`: Returns the midpoint between two points.
6. `lerp(a, b, t)`: Returns the linear interpolation between two values.
7. `clamp(value, min, max)`: Clamps a value within a range.
8. `signum(value)`: Returns the sign of a number.
9. `sqrt(value)`: Returns the square root of a number.
10. `round(num)`: Returns the rounded value of a number.
11. `ceil(num)`: Returns the ceiling of a number.
12. `absolute(num)`: Returns the absolute value of a number.
13. `gcd(a, b)`: Returns the Greatest Common Divisor of two numbers.
14. `lcm(a, b)`: Returns the Least Common Multiple of two numbers.
15. `hypot(p, b)`: Returns the hypotenuse of a right triangle.
16. `randnum(min, max)`: Returns a random number within a range.

STR (String Functions)

1. `strip(str)`: Removes whitespace from a string.
2. `contains(str, search, position)`: Checks if a substring is present in a string.
3. `isBlank(str)`: Checks if a string is blank.
4. `isNotBlank(str)`: Checks if a string is not blank.
5. `isAlphabet(str)`: Checks if a string contains only alphabetic characters.
6. `isNumeric(str)`: Checks if a string contains only numeric characters.
7. `echo(str, n)`: Repeats a string n times.
8. `capitalise(str)`: Capitalizes the first letter of a string.

Here is the updated text with markup:

Examples

Array Functions

Union of two arrays

```
const arr1 = [1, 2, 3];
const arr2 = [3, 4, 5];
const union = SWOOP.ARR.union(arr1, arr2); // [1, 2, 3, 4, 5]
```

Sum of an array

```
const arr = [1, 2, 3, 4, 5];
const sum = SWOOP.ARR.sum(arr); // 15
```

Median of an array

```
const arr = [1, 3, 5, 7, 9];
const median = SWOOP.ARR.median(arr); // 5
```

DOM Functions

Select an element by its ID

```
const elem = SWOOP.DOM.selectid('myElement');
```

Add an event listener to an element

```
const elem = SWOOP.DOM.selectid('myElement');
SWOOP.DOM.addListener(elem, 'click', () => console.log('Clicked!'));
```

Functional Programming Utilities

Throttle a function

```
const throttledFunc = SWOOP.FUNC.throttle(() => console.log('Hello!'), 1000);
throttledFunc(); // Hello!
throttledFunc(); // (no output, due to throttling)
```

Memoize a function

```
const memoizedFunc = SWOOP.FUNC.memoize((x) => x * x);
console.log(memoizedFunc(2)); // 4
console.log(memoizedFunc(2)); // 4 (cached result)
```

Mathematical Functions

Calculate the distance between two points

```
const x1 = 1;
const y1 = 2;
const x2 = 4;
const y2 = 6;
const distance = SWOOP.MATHS.distance(x1, x2, y1, y2); // 5
```

Calculate the sine of an angle

```
const angle = Math.PI / 2;
const sine = SWOOP.MATHS.sin(angle); // 1
```

String Functions

Remove whitespace from a string

```
const str = ' Hello World ';
const stripped = SWOOP.STR.strip(str); // 'HelloWorld'
```

Check if a string contains only alphabetic characters

```
const str = 'HelloWorld';
const isAlphabet = SWOOP.STR.isAlphabet(str); // true
```

Swoophysix Class

The Swoophysix class provides a collection of methods for converting between different units of measurement and calculating various physics-related values.

Constructor

- `constructor(gravity=9.81)`: Initializes the Swoophysix object with a default gravity value of 9.81 m/s^2.

Heat Conversion Methods

Temperature Conversions

- `convertFtoC(value)`: Converts a temperature value from Fahrenheit to Celsius.
- `convertCtoF(value)`: Converts a temperature value from Celsius to Fahrenheit.
- `convertCtoK(value)`: Converts a temperature value from Celsius to Kelvin.
- `convertKtoC(value)`: Converts a temperature value from Kelvin to Celsius.
- `convertFtoK(value)`: Converts a temperature value from Fahrenheit to Kelvin.
- `convertKtoF(value)`: Converts a temperature value from Kelvin to Fahrenheit.

Length Conversion Methods

Length Conversions

- `convertMtoFT(value)`: Converts a length value from meters to feet.
- `convertFTtoM(value)`: Converts a length value from feet to meters.
- `convertINtoCM(value)`: Converts a length value from inches to centimeters.
- `convertCMtoIN(value)`: Converts a length value from centimeters to inches.
- `convertMtoIN(value)`: Converts a length value from meters to inches.
- `convertINtoM(value)`: Converts a length value from inches to meters.
- `convertCMtoM(value)`: Converts a length value from centimeters to meters.
- `convertMtoCM(value)`: Converts a length value from meters to centimeters.

Physics Calculation Methods

Physics Calculations

- `force(mass, acceleration)`: Calculates the force applied to an object.
- `pressure(force, area)`: Calculates the pressure exerted on an object.
- `energy(mass, velocity)`: Calculates the kinetic energy of an object.
- `momentum(mass, velocity)`: Calculates the momentum of an object.
- `liquid_pressure(area, height, density)`: Calculates the pressure exerted by a liquid.
- `friction(force, coeff)`: Calculates the frictional force applied to an object.
- `acceleration(velocity, time)`: Calculates the acceleration of an object.
- `potential_energy(mass, height)`: Calculates the potential energy of an object.
- `torque(radii, force)`: Calculates the torque applied to an object.

Examples

```
const swoophysix = new Swoophysix();

console.log(swoophysix.convertFtoC(32)); // 0
console.log(swoophysix.convertMtoFT(1)); // 3.28084
console.log(swoophysix.force(10, 2)); // 20
```

SwoopCache Class

The SwoopCache class provides a simple caching mechanism with a built-in time-to-live (TTL) feature.

Constructor

- `constructor(TTL=60000)`: Initializes the SwoopCache object with a default TTL of 60 seconds (60000 milliseconds).

Methods

setCacheData(key, value)

- Sets a cache entry with the given key and value. The entry will expire after the TTL has passed.

getCachedData(key)

- Retrieves a cache entry by its key. If the entry has expired, it will be deleted and null will be returned.

deleteCachedData(key)

- Deletes a cache entry by its key.

clearCachedData()

- Clears all cache entries.

Example Usage

```
const cache = new SwoopCache(30000); // 30-second TTL

cache.setCacheData('key', 'value');
console.log(cache.getCachedData('key')); // 'value'

// Wait for 30 seconds...

console.log(cache.getCachedData('key')); // null (expired)

cache.deleteCachedData('key');
cache.clearCachedData();
```

SwoopTest Class

The SwoopTest class provides a simple testing framework for writing and running unit tests.

Constructor

- `constructor()`: Initializes the SwoopTest object with an empty array of tests.

Methods

addTest(name, fn)

- Adds a test to the test suite with the given name and test function.

runTests()

- Runs all tests in the test suite, logging the results to the console.

Assertion Methods

- `assertEqual(a, b)`: Throws an error if `a` is not equal to `b`.
- `assertNotEqual(a, b, message)`: Throws an error if `a` is equal to `b`.
- `assertGreaterThan(a, b, message)`: Throws an error if `a` is not greater than `b`.
- `assertLessThan(a, b, message)`: Throws an error if `a` is not less than `b`.

Example Usage

```
const test = new SwoopTest();

test.addTest('test equal', () => {
test.assertEqual(1, 1);
});

test.addTest('test not equal', () => {
test.assertNotEqual(1, 2);
});

test.addTest('test greater than', () => {
test.assertGreaterThan(2, 1);
});

test.addTest('test less than', () => {
test.assertLessThan(1, 2);
});

test.runTests();
```

This will output:

```
passed test equal ✓
passed test not equal ✓
passed test greater than ✓
passed test less than ✓
```

Contributing Guidelines

We welcome contributions to the Swoop Library! If you'd like to get involved, here are some guidelines to help you get started:

_Reporting Issues_

- If you encounter a bug or issue, please open an issue on our GitHub page.
- Provide a clear description of the issue, along with any relevant code or examples.

_Submitting Pull Requests_

- If you'd like to contribute code, please submit a pull request.
- Ensure your code is well-documented and follows our coding standards.
- Provide a clear description of the changes you've made.

_Coding Standards_

- We follow standard JavaScript coding conventions.
- Keep code concise, readable, and well-documented.

_Testing_

- Ensure all changes are tested thoroughly.
- Add new tests as needed to cover new functionality.

_License_

- By contributing, you agree to license your contributions under the Apache 2.0 License.
- Please note that this license allows for free use, modification, and distribution of the software.

_Thanks!_

We appreciate your contributions to the Swoop Library! If you have any questions or need help getting started, feel free to reach out.