ywfts.js

2.1.2 • Public • Published

What is ywfts

ywfts is a package that gives you some cool features, like database and some tools you can use that can help your life. It was created to be as simple yet as full of features as possible. The source code is public so you can see any time! The database is json based by the way.

Database

How to access the database?

Database class

to access the database, we are using the Database class, that gives us all of the functions about the database

const ywfts = require("ywfts.js")
const db = new ywfts.Database('Name is optional!')

Database functions

<db> is what you defined as ywfts.Database()

  • <db>.set(key, value) will set a specific value to the key, keep in mind this key as you will use it everytime you want to access that key's value
  • <db>.get(key) will get the value for this specific key, if there is no value, it will return undefined
  • <db>.add(key, value) will add a value to a key, only works with numbers and STRINGS ONLY CONTAINING NUMBERS.
  • <db>.subtract(key, value) Subtracts a specific value from the key's value
  • <db>.delete(key) will delete the key from the database
  • <db>.clear() will clear the entire database for you
  • <db>.push(key, value) will push the value to an array stored in the key, if there is no value on the key, it will set the key's value an array with only the value inside.
  • <db>.removeFromArray(key, value) will remove from that key's array a specific value, so you don't have to.
  • <db>.readAll() will return the entire file's data. Everything.
  • <db>.addGitIgnore() will add the database's file name to a .gitignore file, if there is already one, it will overwrite the content as lastcontent + new line + database file

Example usage

set example

This will set bread with a value of 500 and then will print the value of bread

index.js

db.set("bread", 500)
console.log(db.get("bread"))

data.json

{"bread": 500 }

console

500

add example

This will add to our past key bread a value of 100

index.js

db.add("bread", 100)
console.log(db.get("bread"))

data.json

{"bread": 600}

console

600

push example

this will push the string 'This is a string' to an array, now there is no array on the key's value yet, so the database will create one for with that value already.

index.js

db.push("myArray", 'This is a string')
console.log(db.get("myArray"))

data.json

{"myArray": ["This is a string"]}

console

['This is a string']

everytime you run this, it will add the same string to the array.

Removing something from the array example

this will remove from our past key myArray the string This is a string, but when we remove the only string from the array, all we have left is an empty array like [], however if you ran 2 times the code, you noticed there is 2 of the same strings on the array, so in that case, it would remove the first one, leaving you with only ['This is a string'] array.

index.js

db.removeFromArray("myArray", 'This is a string')
console.log(db.get("myArray"))

data.json

{"myArray": []}

console

[]

deleting an item / clearing the database

let's delete the item rock and read our current database, after that, we are going to clear the entire database and check how it looks

index.js

db.delete("rock")
console.log(db.readAll())
db.clear()
console.log(db.readAll())

data.json

before deleting "rock"

{"anotherrock": 500, "rock": "Not dwayne the rock johnson"}

after deleting "rock"

{"anotherrock": 500}

after clearing the database

{}

console

{"anotherrock": 500}
{}

Tools

even though this is not the main reason this package exists, there is 3 current tools that can be usefull in case you don't want to create a custom function just to make a specific task.

How to access tools?

Almost the same as database, but instead we just need to replace it with Tools, like this:

const ywfts = require("ywfts.js")
const tools = new ywfts.Tools()

there are no extra options for tools btw.

Tools functions

<tools> is what you defined as ywfts.Tools()

  • <tools>.removeFromObject(object, value) Removes a specific value from the object
  • <tools>.removeFromArray(array, value) Removes a specific value frmo the array
  • <tools>.calcOnString(number1, operation, number2) Works when you need to calculate numbers inside strings like '100' + '200', because normally this would return 100200 and not 300, but with this method it does return 300

Example usage

removing something from array

let's have the array [200, 400, 100, 50] and we want to have only an array with the values of [200, 100, 50], which means we have to remove the 400.

index.js

let array = [200, 400, 100, 50]
tools.removeFromArray(array, 400)
console.log(array)

console

[ 200, 100, 50 ]

removing something from an object

let's say we have the object { "name": "something", "bread": true } and we want ONLY bread to be on the object

index.js

let obj = { "name": "something", "bread": true }
tools.removeFromObject(obj, 'name')
console.log(obj)

console

{ "bread": true }

calc with strings

ok so we have the strings '100' and '200' and we want to add only the numbers inside, so instead of '100200' we want 300 to be the output

index.js

console.log(tools.calcOnString('100', '+', '200'))

console

300

What's new?

2.0.0

  • Big change on the setup
  • Bug fixes on some functions.
  • Fixed readme.md

2.0.1

  • Bug Fixed! (icon was too big)

2.0.2

  • Removed the object "value", so now it consumes less space, but the same efficency
  • You can now GET a value of a key inside an object inside a key, something like this:

index.js

db.get("myObject.something")

data.json

{"myObject": { "something": "myValue" }}

2.1.0

  • There is now complete support for objects, so now you can set stuff directly on an object inside a key, like this:

index.js

db.set('myObject.myParam', 10)

data.json

{"myObject": {"myParam": 10}}

2.1.1

  • Fixed subtract function error, where it would create a new key and not subtract the value at all.

2.1.2

  • Fixed error messages and readme, also saw that ywfts is on yarn so added to links the yarn package link

Ready to go!

well, with some of these examples and some creativity, you can create some cool stuff! if you have any error, bug or any ideia you want to share, please join our discord server!

Links

Yarn package link

YWFTS.js (Community / Support)

https://nodei.co/npm/ywfts.js.png?downloads=true&downloadRank=true&stars=true

Package Sidebar

Install

npm i ywfts.js

Weekly Downloads

1

Version

2.1.2

License

ISC

Unpacked Size

20.9 kB

Total Files

6

Last publish

Collaborators

  • withoutideias