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 advanced and more specific utility classes, such as:
You can simply include Swoop.js using our CDN link
or download it from here
Let's get started with Swoop.js! Include Swoop.js in your source code to get started:
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.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 the given gravity value (default is 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.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.The SwoopTest class provides a simple testing framework for writing and running tests.
constructor()
Initializes the SwoopTest object with an empty array of tests.
addTest(name, fn)
Adds a new test to the array of tests. The test is defined by a name and a function that contains the test logic.
runTests()
Runs all the tests in the array of tests. If a test passes, it logs a success message to the console. If a test fails, it logs an error message to the console.
The following assertion methods can be used within tests to verify expected results:
assertEqual(a, b)
: Verifies that two values are equal. If not, throws an error.assertNotEqual(a, b, message)
: Verifies that two values are not equal. If they are, throws an error.assertGreaterThan(a, b, message)
: Verifies that the first value is greater than the second. If not, throws an error.assertLessThan(a, b, message)
: Verifies that the first value is less than the second. If not, throws an error.The SwoopCache class provides a simple caching mechanism with a time-to-live (TTL) feature.
constructor(TTL=60000)
Initializes the SwoopCache object with an empty cache object and a default TTL of 60 seconds (60000 milliseconds).
setCacheData(key, value)
Sets a new cache entry with the given key and value. The entry will expire after the TTL period.
getCachedData(key)
Retrieves the cached value for the given key. If the entry has expired or does not exist, returns null.
deleteCachedData(key)
Deletes the cached entry for the given key.
clearCachedData()
Clears all cached entries.
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
Throttling A Function
SWOOP.FUNC.throttle(myFunc(),2000)
/*allows the function to be called once in 2 seconds,i.e,2000 milliseconds*/
convert metres to centimetres using swoophysix
const physix = new Swoophysix();
physix.convertMtoCM(20)
//converts 20 metres to centimetres
using SwoopCache to cache data
const cache = new SwoopCache(TTL=60000)//the cached data resets after 1 minute
cache.setCacheData("myKey","myValue") //sets a data in the cache
//obtaining cached data
cache.getCachedData("myKey")
//you can delete cache data like this:
cache.deleteCachedData("myKey")
Swoop.js has just started its journey, and its features may be limited, but we believe that together, we can make it the best JavaScript library in the world. We highly appreciate contributions and look forward to collaborating with you to achieve this goal.
If you'd like to contribute, please follow these guidelines:
If you find a bug or have a feature request, please submit an issue on our GitHub page. Make sure to include as much detail as possible, such as:
Swoop.js is licensed under the Apache 2.0 License. This means you can use, modify, and distribute the library freely, as long as you include the original copyright notice and license text in your project.
Swoop.js uses semantic versioning. This means that versions are incremented as follows:
Swoop.js is a standalone project, built from scratch using pure JavaScript.