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.
<script src="https://cdn.jsdelivr.net/gh/SomnathDevPro/Swoop.js@main/swoop.min.js"></script>
The SWOOP
object is the main object which you will be using to access the basic utilities!
available utility functions:
union(arr1, arr2)
: Returns the union of two arrays.intersection(arr1, arr2)
: Returns the intersection of two arrays.difference(arr1, arr2)
: Returns the difference of two arrays.sum(arr)
: Returns the sum of all elements in an array.mean(arr)
: Returns the mean of an array.standard_deviation(arr)
: Returns the standard deviation of an array.variance(arr)
: Returns the variance of an array.median(arr)
: Returns the median of an array.min(arr)
: Returns the minimum value of an array.max(arr)
: Returns the maximum value of an array.countUnique(arr)
: Returns the number of unique values in an array.countDuplicates(arr)
: Returns the number of duplicate values in an array.merge(arr1, arr2)
: Merges two arrays.freq(arr)
: Returns the frequency of elements in an array.
select(selector)
: Selects an element by its selector.selectid(id)
: Selects an element by its ID.selectall(element)
: Selects all elements with a matching selector.addListener(element, event, fn)
: Attaches an event listener to an element.removeListener(element, event, fn)
: Removes an event listener from an element.
throttle(func, wait)
: Throttles a function to prevent excessive calls.debounce(func, wait)
: Debounces a function to prevent excessive calls.memoize(func)
: Memoizes a function to cache its results.curry(func)
: Curries a function to enable partial application.once(func)
: Ensures a function is called only once.
sin(angle)
: Returns the sine of an angle.cos(angle)
: Returns the cosine of an angle.tan(angle)
: Returns the tangent of an angle.distance(x1, x2, y1, y2)
: Returns the distance between two points.midpoint(x1, x2, y1, y2)
: Returns the midpoint between two points.lerp(a, b, t)
: Returns the linear interpolation between two values.clamp(value, min, max)
: Clamps a value within a range.signum(value)
: Returns the sign of a number.sqrt(value)
: Returns the square root of a number.round(num)
: Returns the rounded value of a number.ceil(num)
: Returns the ceiling of a number.absolute(num)
: Returns the absolute value of a number.gcd(a, b)
: Returns the Greatest Common Divisor of two numbers.lcm(a, b)
: Returns the Least Common Multiple of two numbers.hypot(p, b)
: Returns the hypotenuse of a right triangle.randnum(min, max)
: Returns a random number within a range.
strip(str)
: Removes whitespace from a string.contains(str, search, position)
: Checks if a substring is present in a string.isBlank(str)
: Checks if a string is blank.isNotBlank(str)
: Checks if a string is not blank.isAlphabet(str)
: Checks if a string contains only alphabetic characters.isNumeric(str)
: Checks if a string contains only numeric characters.echo(str, n)
: Repeats a string n times.capitalise(str)
: Capitalizes the first letter of a string.
Here is the updated text with markup:
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
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!'));
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)
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
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
The Swoophysix class provides a collection of methods for converting between different units of measurement and calculating various physics-related values.
constructor(gravity=9.81)
: Initializes the Swoophysix object with a default gravity value of 9.81 m/s^2.
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.
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.
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.
const swoophysix = new Swoophysix();
console.log(swoophysix.convertFtoC(32)); // 0
console.log(swoophysix.convertMtoFT(1)); // 3.28084
console.log(swoophysix.force(10, 2)); // 20
The SwoopCache class provides a simple caching mechanism with a built-in time-to-live (TTL) feature.
constructor(TTL=60000)
: Initializes the SwoopCache object with a default TTL of 60 seconds (60000 milliseconds).
- Sets a cache entry with the given key and value. The entry will expire after the TTL has passed.
- Retrieves a cache entry by its key. If the entry has expired, it will be deleted and null will be returned.
- Deletes a cache entry by its key.
- Clears all cache entries.
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();
The SwoopTest class provides a simple testing framework for writing and running unit tests.
constructor()
: Initializes the SwoopTest object with an empty array of tests.
- Adds a test to the test suite with the given name and test function.
- Runs all tests in the test suite, logging the results to the console.
assertEqual(a, b)
: Throws an error ifa
is not equal tob
.assertNotEqual(a, b, message)
: Throws an error ifa
is equal tob
.assertGreaterThan(a, b, message)
: Throws an error ifa
is not greater thanb
.assertLessThan(a, b, message)
: Throws an error ifa
is not less thanb
.
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 ✓
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.