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

@htmlguyllc/jpack

Package Overview
Dependencies
Maintainers
1
Versions
191
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@htmlguyllc/jpack

Core Javascript Library of Everyday Objects, Events, and Utilities

  • 0.1.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
19
increased by533.33%
Maintainers
1
Weekly downloads
 
Created
Source

jPack

jPack is a library of components, objects, plugin wrappers, and utilities designed to make building custom websites simpler.

With jPack, you can easily upgrade your server-side rendered application to a pseudo-SPA using XHR requests for page-loads, get values from the querystring, store and interact with user/multi-tenant website data, and more.

Installation

With Yarn or NPM:

yarn add htmlguyllc-jpack;
//or
npm i htmlguyllc-jpack;

and then use what you need, where you need it (requires ES6):

//a single component
import {navigation} from 'htmlguyllc-jpack/components/navigation';
navigation.load('/my-page');

//or a namespaced object containing all components
import 'htmlguyllc-jpack/components';
components.navigation.load('/my-page');

//or a namespaced object containing all types
import 'htmlguyllc-jpack';
jpack.components.navigation.load('/my-page');

//or extract multiple
import {user, site} from 'htmlguyllc-jpack/objects';
user.getId();
site.getId();

Or you can download the latest release, unzip it, put it in your public folder then include the whole library:

<script href="/vendor/htmlguyllc-jpack/dist/jpack.min.js">
<script>
window.addEventListener('load', function() {
    jpack.components.navigation.load('/my-page');
};
</script>

What's Included:

Four categories of functionality are provided in this library. Each has it's own namespace in parenthesis below.

Components (components):

None yet.

Objects (objects):

request, site, user

Plugin Wrappers (plugin_wrappers):

None yet.

Utilities (utilities):

strings, data_types, dom, events

Documentation

- Components -

None yet.

- Objects -

-Request

Provides a wrapper for window.location and query string access

Method/PropertyParamsReturnNotes
queryn/aURLSearchParamssee https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#Methods
isHttpsbool
getDomainstring
getDomainWithProtocolstring
getURIstringalso known as the path - does not include querystring
getURIWithQueryStringstringfull URL after the domain
getFullURLstring
appendSlashstringstringadds a slash (if there isn't already one) to the end of a string.
To use:
import {request} from 'htmlguyllc-jpack/objects'; 

//get product_id from the querystring
var product_id = request.query.get('product_id');

var current_full_url = request.getFullURL();

var full_blog_url = request.appendSlash(request.getDomainWithProtocol())+'blog'; //https://my-domain.com/blog

-Site

Designed for multi-tenant applications, this object stores a site's id, name, and config object.

Method/PropertyParamsReturnNotes
getIdstring/int
setIdstring/intthis
getNamestringthis
setNamestringthis
getConfigobject/arraythis
setConfigobject/arraythisoverwrites all config
getConfigItemmixedreturns an individual item from config
setConfigItemstring,mixedthissets the value of an individual item in config
populateobjectthissets provided values all at once (id,name,config)
To populate with data:

The easy way: Create an object named $site with values from your server (prior to including jpack)

<script>
const $site = {
    id: <?php echo $site['id']; ?>,
    name: "<?php echo $site['name']; ?>",
    //$site['config'] is a key/val array of configuration options
    config: JSON.parse("<?php echo json_encode($site['config']); ?>"),
};
</script>

The harder way: Perform an XHR request to grab site details via a JSON API, then run the populate method on the site object.

import {site} from 'htmlguyllc-jpack/objects';
 
$.get('/my-site-info-endpoint.php', function(data){
    //don't forget error handling!
    site.populate(JSON.parse(data));
});

-User

Designed for sites with user accounts/guest accounts. This object stores a user's details and allows for front-end permission checks.

Method/PropertyParamsReturnNotes
getIdstring/int
setIdstring/intthis
getIsGuestboolif your site has users who don't login but still interact and have a user record (like guest checkout)
setIsGuestboolthis
setIsAdminboolif your site has users who have ultimate permissions and you want to do something based on that
setIsGuestboolthis
getUsernamestring
setUsernamestringthis
getFnamestring
setFnamestringthis
getLnamestring
setLnamestringthis
getNamestringreturns fname and lname concatenated with a space
getEmailstring
setEmailstringthis
getPhonestring
setPhonestringthis
getPermissionsarray
setPermissionsarraythis
addPermissionstring/intthis
removePermissionstring/intthis
hasPermissionstring/intbool
getAdditionalDataobject/arrayset any additional data about this user that doesn't fit the default getters and setters here (a better idea would be to extend this object with your custom properties/methods)
setAdditionalDataobject/arraythis
populateobjectthissets provided values all at once (id, isGuest, isAdmin, etc)
To populate with data:

The easy way: Create an object named $user with values from your server (prior to including jpack)

<script>
const $user = {
    id: <?php echo $user['id']; ?>,
    fname: "<?php echo $user['fname']; ?>",
    //..
    permissions: JSON.parse("<?php echo json_encode($user['permissions']); ?>"),
    additionalData: JSON.parse("<?php echo json_encode([
        'user_type'=>$user['user_type'],
        //whatever else you might want to pass
    ]); ?>"),
    //..
};
</script>

The harder way: Perform an XHR request to grab site details via a JSON API, then run the populate method on the site object.

import {user} from 'htmlguyllc-jpack/objects';
 
$.get('/my-user-info-endpoint.php', function(data){
    //don't forget error handling!
    user.populate(JSON.parse(data));
});
To use:
import {site} from 'htmlguyllc-jpack/objects';

var site_id = site.getId();

- Plugin Wrappers -

None yet.

- Utilities -

-Strings

Common string manipulations

Method/PropertyParamsReturnNotes
ucfirststringstringcapitalizes the first letter of a string like ucfirst in PHP
getterstringstringcreates a getter method name from a string
setterstringstringcreates a setter method name from a string
To Use:
import {strings} from 'htmlguyllc-jpack/utilities';

strings.ucfirst('bob'); //returns 'Bob'
strings.getter('name'); //returns 'getName';
strings.setter('name'); //returns 'setName';

-DOM

HTML DOM helpers

Method/PropertyParamsReturnNotes
getDomElementmixed, bool, boolElement/HTMLDocument/nullreturns a native DOM element for whatever you provide (selector string, array of elements, single element, jQuery wrapped DOM element, etc)
getDomElementsmixed, boolarraysame as getDomElement, except it returns all matches
existsmixedboolchecks to see if it exists in the DOM
multipleExistmixedboolchecks to see if more than 1 instance exists in the DOM
To Use:
import {dom} from 'htmlguyllc-jpack/utilities';

//Dont do this. Most of these are dumb examples.
dom.getDomElement('.my-fav-button', true, true); //will throw an error if it doesn't fine it, or if it finds more than 1
dom.getDomElements('.links', true); //will throw an error if none are found
dom.getDomElement('.my-button'); //returns the button, or null (if multiple, it returns the first)
dom.getDomElements('.links'); //returns an array of any matches for .links
dom.getDomElement($('a')); //returns the native DOM element for the link and removes the jQuery wrapper
dom.getDomElement(document.querySelectorAll('a')); //returns the first anchor

dom.exists('a'); //returns true if there is an anchor on the page
dom.multipleExist('a'); //returns true if more than 1 anchor on the page

-Type Checks

Check the data type of a value with more specificity than typeof or vanilla JS functions

Method/PropertyParamsReturnNotes
isDataObjectobject, array, bool, bool, boolboolvalidates that an object contains data and not a dom element, array, null or anything else that would normally return true when you call typeof
To Use:
import {type_checks} from 'htmlguyllc-jpack/utilities';

var my_obj = {id:null, name:'John Doe', email:'john@doe.com'};

//make sure my_obj contains the keys: 'id', 'name', 'email'
//force all keys to exist
//block any keys that aren't in that list
//throw an error if the object fails any checks
type_checks.isDataObject(my_obj, ['id', 'name', 'email'], true, true, true);

-Events

Shorthand event handlers

Method/PropertyParamsReturnNotes
onClickmixed, functionarrayprevents the browser's default so you can handle link clicks and form submissions with less code
offClickmixed, functionarrayremoves the handler you added using onClick
onSubmitmixed, functionarraysame as .onClick() but for submit
offSubmitmixed, functionarraysame as .offClick() but for submit
onChangemixed, functionarrayadds an on change handler but does NOT preventDefault - mostly exists for consistency
offChangemixed, functionarrayremoves the handler you added using .onChange()
onEventPreventDefaultmixed, string, functionarrayattaches an event handler and prevents the default browser action
offEventPreventDefaultmixed, string, functionarrayremoves the handler you attached with .onEventPreventDefault()
To Use:
import {events} from 'htmlguyllc-jpack/utilities';

events.onClick('a.my-link', function(){
   //do something without the page redirecting to the href 
});

events.onSubmit('form.my-form', function(){
   //do something and submit the form using XHR 
});

FAQs

Package last updated on 12 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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