npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

d9attributes

v1.0.1

Published

Node js library for drupal 9 attribute object emulation.

Downloads

4

Readme

D9Attributes

Node js library for drupal attribute object emulation.

drupal-attribute

Node js library for drupal attribute object emulation.

Main idea is using this library in projects with twig templates for components organize, like StoryBook or etc.

Installation

npm install d9attributes

How to use.

In my case, I combine the Storybook React and Storybook HTML versions(I know this method is not preferred, but I need this in my task).

First one - add to twig the function createAttribute and made fix for filter without in the file .storybook/preview.js:

import Twig from 'twig';
const { Attribute } = require('d9attributes')

const createAttribute = (data) => new Attribute(data);

const without = function (element) {
  if (!element) {
    return []
  }

  const filteredElement = Object.assign(Object.create(Object.getPrototypeOf(element)), element)

  let args = Array.prototype.slice.call(arguments, 1)
  if (args[0]) {
    for (let name of Object.keys(filteredElement)) {
      if (args[0].includes(name)) {
        delete filteredElement[name]
      }
    }
  }
  return filteredElement
}

...

Twig.extendFunction('create_attribute', createAttribute);
Twig.extendFilter('without', without);

Second one - create the React component Template(utils/template.js) with code:

import React from 'react'
import parse from 'html-react-parser'
import { Attribute } from 'd9attributes'

class Template extends React.Component {
  constructor (props) {
    super(props);
  }
  checkAttributes = (args) => {
    const attributes = ['attributes', 'title_attributes'];
    const items = ['items', 'below'];
    const self = this;
    if (typeof args !== 'object') {
      return args;
    }
    Object.keys(args).forEach((key) => {
      if (items.indexOf(key, key.toLowerCase()) !== -1 && args[key].length) {
        args[key].forEach((item) => self.checkAttributes(item))
      }
      else if (attributes.indexOf(key, key.toLowerCase()) !== -1) {
        args[key] = new Attribute(args[key]);
      }
    })
    if (typeof args.attributes === 'undefined') {
      args.attributes = new Attribute();
    }
  }
  render () {
    let args = this.props.data;
    this.checkAttributes(args);
    return parse(this.props.template(args));
  }
}

export default Template;

Third one - add the namespace for utils folder to the main.js(.storybook/main.js) file:

module.exports = {
  
  ...
  
  "webpackFinal": async (config, { configType }) => {

    // Alias
    config.resolve.alias = {
      '@utils': path.resolve(__dirname, '..', 'utils'),
      
      ...
      
    }
    
    ...
    
  }
}

Firth one - use your Template component for your components in the *.stories.js files, for exapmle:

// Import twig file.
import template from './template.twig'
// Import twig data.
import data from './block.yaml'
// Import own component.
import Template from '@utils/template'

...

const Html = args => <Template template={template} data={args}/>

export const BlockTemplate = Html.bind({})
BlockTemplate.args = data
...

Thank you for your attention :)

License

Apache-2.0 © Serhii Yallin