Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript declarations, RemoveEmptyLines, TrimLines, InsertFinalNewLine #33

Merged
merged 9 commits into from
Jun 14, 2022

Conversation

scripthunter7
Copy link
Member

@scripthunter7 scripthunter7 commented Jun 2, 2022

  • TypeScript declarations + JSoc
    • Suggestions in VSCode:
      image
  • RemoveEmptyLines
    • Input:
      rule1
      
      rule2
      
      
      rule3
      
    • Output:
      rule1
      rule2
      rule3
      
  • TrimLines
    • Input:
      rule1
        rule2
              rule3
          rule4
      
    • Output:
      rule1
      rule2
      rule3
      rule4
      
  • InsertFinalNewLine
    • Input:
      rule1
      rule2
      
    • Output:
      rule1
      rule2
      
      

Test example

Input file test.txt:


        ||ads.geeks.hu^     
||adv.forpsi.hu^
||adv.hu^       
                ||adverbs.napilapcsoport.hu^
||advert*.ultraweb.hu^

! random comment


            ||adverticum.*.hu^
||app.livechatoo.com^
||as*.prog.hu^
! duplicate
||audit.median.hu^
||audit.median.hu^

||avatraffic.com^
||banner.terminal.hu^
||banner.webtar.hu^

Test script: hostlist-compiler-test.ts

import compile from '@adguard/hostlist-compiler';
import { writeFileSync } from 'fs';

(async () => {
    // Compile filters
    const result = await compile({
        name: 'Test Output',
        sources: [
            {
                type: 'adblock',
                source: 'test.txt',
                // Transformations for the imported source
                transformations: ['RemoveComments', 'Validate'],
            },
        ],
        // Transformations for the whole list
        transformations: ['Deduplicate', 'RemoveEmptyLines', 'TrimLines', 'InsertFinalNewLine'],
    });

    // Write to file
    writeFileSync('test-output.txt', result.join('\n'));
})();

Output file test-output.txt:

!
! Title: Test Output
! Last modified: 2022-06-02T22:08:39.598Z
!
! Compiled by @adguard/hostlist-compiler v1.0.14
!
!
! Source: release/test.txt
!
||ads.geeks.hu^
||adv.forpsi.hu^
||adv.hu^
||adverbs.napilapcsoport.hu^
||advert*.ultraweb.hu^
||adverticum.*.hu^
||app.livechatoo.com^
||as*.prog.hu^
||audit.median.hu^
||avatraffic.com^
||banner.terminal.hu^
||banner.webtar.hu^

"Real life" usage

hufilter-dns.ts:

import HostlistCompiler, { IConfiguration as HostlistCompilerConfiguration } from '@adguard/hostlist-compiler';
import { writeFileSync } from 'fs';

;(async () => {
    // Configuration
    const config: HostlistCompilerConfiguration = {
        name: 'hufilter DNS filter',
        description: 'Simplified hufilter version specifically to be better compatible with DNS-level ad blocking.',
        homepage: 'https://github.com/hufilter/hufilter/wiki',
        license: 'https://github.com/hufilter/hufilter/blob/master/LICENSE',
        sources: [
            {
                type: 'adblock',
                source: 'https://raw.githubusercontent.com/hufilter/hufilter/master/hufilter-adguard.txt',
                // Transformations for the imported source
                transformations: ['RemoveComments', 'Validate'],
            },
        ],
        // Transformations for the whole list
        transformations: ['Deduplicate', 'RemoveEmptyLines', 'TrimLines', 'InsertFinalNewLine'],
    };

    // Compile
    const result = await HostlistCompiler(config);

    // Write to file
    writeFileSync('hufilter-dns.txt', result.join('\n'));
})();

Related issues

@ameshkov
Copy link
Member

ameshkov commented Jun 3, 2022

@scripthunter7 thank you for the contribution! @tvinzz will take care of it.

* @returns {Array<string>} filtered lines/rules
*/
function trimLines(lines) {
const transformed = lines.map((line) => _.trim(line, [' ', '\t']));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scripthunter7 lodash _.trim function for the second argument takes string, not array
with the array, it implicitly trims commas in the lines too, though there are no such cases in the tests

Copy link

@maximtop maximtop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix

@scripthunter7
Copy link
Member Author

please fix

@maximtop Thank you! Fixed, see dbf44ad

@adguard adguard merged commit dbf44ad into AdguardTeam:master Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants