Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

contactsdatasource

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contactsdatasource - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

5

index.js
import LocalStorageManager from './localStorage';
const localStorageManager = null;
export default CreateDataSource = (name='contacts', sourceType='localStorage') => {
const CreateDataSource = (name='contacts', sourceType='localStorage') => {
switch(sourceType){

@@ -11,2 +11,3 @@ default:

}
}
}
export default CreateDataSource;

124

localStorage.js
class LocalStorageManager {
constructor(){
constructor() {
}

@@ -8,28 +9,119 @@ /*

*/
createStore(name){
if(!localStorage[name]){
createStore(name) {
this._storageName = name || 'humanitec';
if (!localStorage[this._storageName]) {
const data = {
contacts: [{
id:1,
name:'Shahzad',
age:27,
gender:'M',
email:'shahzad31comp@gmail.com',
city:'ISL',
country:'PK'
id: Date.now(),
name: 'Shahzad',
age: 27,
gender: 'M',
email: 'shahzad31comp@gmail.com',
city: 'ISL',
country: 'PK'
}]
}
localStorage[name]=JSON.stringify(data);
};
localStorage[this._storageName] = JSON.stringify(data);
}
//returning promise to make it async, because in real life it will probably use async operations
return new Promise((resolve, reject) => {
if (localStorage[name]){
resolve(JSON.parse(localStorage[name]));
}else{
if (localStorage[this._storageName]) {
// add a fake loading time
setTimeout(() => {
resolve(JSON.parse(localStorage[this._storageName]));
}, 2000);
} else {
reject('Unexpected error');
}
})
});
}
_getContacts() {
const data = JSON.parse(localStorage[this._storageName]);
return data.contacts;
}
_updateContacts(contacts) {
localStorage[this._storageName] = JSON.stringify({ contacts: contacts });
}
getContact(id) {
const contacts = this._getContacts();
// find that contact return it
return new Promise((resolve, reject) => {
const contact = contacts.find((item) => (
item.id === id
))
if (contact)
resolve(contact);
else
reject('Contact does not exist');
});
}
addContact(newContact) {
const contacts = this._getContacts();
newContact.id = Date.now();
contacts.push(newContact);
this._updateContacts(contacts);
return new Promise((resolve, reject) => {
if (newContact.id) {
resolve(newContact);
} else {
reject('Unexpected error');
}
});
}
updateContact(updateContact) {
const contacts = this._getContacts();
// find that contact and update it in storage
contacts.map((item, index) => (
item.id === updateContact.id ? updateContact : item
));
this._updateContacts(contacts);
return new Promise((resolve, reject) => {
resolve(updateContact);
});
};
removeContact(id) {
const contacts = this._getContacts();
// remove item by filtering, we could also use splice
const updatedContacts = contacts.filter(item => item.id != id)
this._updateContacts(updatedContacts);
};
reset() {
const data = { contacts: [] };
localStorage[this._storageName] = JSON.stringify(data);
return new Promise((resolve, reject) => {
if (localStorage[this._storageName]) {
resolve(JSON.parse(localStorage[_storageName]));
} else {
reject('Unexpected error');
}
});
};
}
export default LocalStorageManager;
{
"name": "contactsdatasource",
"version": "1.0.0",
"version": "1.0.1",
"description": "This a common store to be use to manage contacts",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc