SWOOP.JS - DOCUMENTATION

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 advanced and more specific utility classes, such as:

INSTALLATION

You can simply include Swoop.js using our CDN link

or download it from here

USAGE

Let's get started with Swoop.js! Include Swoop.js in 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.

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 the given gravity value (default is 9.81 m/s^2).

Heat Conversion Methods

Temperature Conversions

Length Conversion Methods

Length Conversions

Physics Calculation Methods

Physics Calculations

SwoopTest Class

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

Constructor

constructor()

Initializes the SwoopTest object with an empty array of tests.

Adding 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.

Running Tests

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.

Assertion Methods

The following assertion methods can be used within tests to verify expected results:

SwoopCache Class

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

Constructor

constructor(TTL=60000)

Initializes the SwoopCache object with an empty cache object and a default TTL of 60 seconds (60000 milliseconds).

Setting Cache Data

setCacheData(key, value)

Sets a new cache entry with the given key and value. The entry will expire after the TTL period.

Getting Cached Data

getCachedData(key)

Retrieves the cached value for the given key. If the entry has expired or does not exist, returns null.

Deleting Cached Data

deleteCachedData(key)

Deletes the cached entry for the given key.

Clearing Cached Data

clearCachedData()

Clears all cached entries.

Examples

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
  
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")
  
    

Contributing

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:

Code Contributions

  1. Fork the repository on GitHub.
  2. Create a new branch for your feature or fix.
  3. Write tests for your changes.
  4. Make sure your code passes all tests.
  5. Submit a pull request.

Documentation Contributions

  1. Fork the repository on GitHub.
  2. Create a new branch for your documentation changes.
  3. Make your changes to the documentation files.
  4. Submit a pull request.

Issue Reporting

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:

Licensing

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.

Versioning

Swoop.js uses semantic versioning. This means that versions are incremented as follows:

Acknowledgments

Swoop.js is a standalone project, built from scratch using pure JavaScript.